Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions PalindromePartitions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// PalindromPartitions.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 4/7/26.
//

class PalindromePartition {

init() {

}

/*
time complexity - n * 2^n (since at every position, we have choice to choose or not choose so, exponent of 2.) We are checking for each and every string if it is palindrome or not. Hence, multiple by n
*/
func partition(_ s: String) -> [[String]] {
var result = [[String]]()
var path = [String]()
let sArr = Array(s)
helper(sArr: sArr, start: 0, path: &path, result: &result)
return result
}

//for loop based recursion
func helper(sArr: [Character], start: Int, path: inout [String], result: inout [[String]]) {

//base
//if we reached end of string then add in the result
if (start == sArr.count) {
result.append(path)
return
}

for i in start..<sArr.count {

//take substring from start to i and check if it is palindrom. If yes, then make further call
let current = Array(sArr[start...i])
if (isPalindrome(sArr: current)) {
path.append(String(current))
helper(sArr: sArr, start: i + 1, path: &path, result: &result)
path.removeLast()
}
}
}

private func isPalindrome(sArr: [Character]) -> Bool {
var i = 0
var j = sArr.count - 1
while (i < j) {
if (sArr[i] == sArr[j]) {
i += 1
j -= 1
} else {
return false
}
}
return true
}

}
95 changes: 95 additions & 0 deletions Subsets.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// Subsets.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 4/7/26.
//

class Subsets {

init() {

}

//using 0-1 recursion
/*
time complexity - 2^n
n - no of elements in array
*/
func subsets(_ nums: [Int]) -> [[Int]] {
var result = [[Int]]()
var path = [Int]()
findSubsets(nums, i: 0, path: &path, result: &result)
return result
}

private func findSubsets(_ nums: [Int], i: Int, path: inout [Int], result: inout [[Int]]) {

//base
if (i == nums.count) {
result.append(path)
return
}

//no choose
findSubsets(nums, i: i + 1, path: &path, result: &result)

//choose
path.append(nums[i])
findSubsets(nums, i: i + 1, path: &path, result: &result)
path.removeLast()
}

//MARK: For-loop based recursion
//using for-loop based recursion
func subsetsUsingForLoop(_ nums: [Int]) -> [[Int]] {
var result = [[Int]]()
var path = [Int]()
findSubsetsUsingForLoop(nums, start: 0, path: &path, result: &result)
return result
}

/*
In for-loop recursion, every node itself represents a valid subset. We must record subset at each node. Not just leaf nodes.
*/
private func findSubsetsUsingForLoop(_ nums: [Int], start: Int, path: inout [Int], result: inout [[Int]]) {

//base
result.append(path)

//logic
for i in start..<nums.count {
path.append(nums[i])
findSubsetsUsingForLoop(nums, start: i + 1, path: &path, result: &result)
path.removeLast()
}
}

//MARK: without recursion
/*
iniitally keep subset as []
now, multiple it with all the numbers then it will become [] , [1], [2], [3]
again multiple by 1, 2, 3 -> [], [1], [2], [3], [1, 2], [2, 3]
then for

one loop on result
another loop on elements of array. Create deep copy of all the elements inside the result
*/

func subsetsUsingIterativeApproach(_ nums: [Int]) -> [[Int]] {
var result = [[Int]]()
result.append([])
for i in 0..<nums.count {
let rSize = result.count
for j in 0..<rSize {
print("result[j] \(result[j])")
var li = result[j]
li.append(nums[i])
print("li \(li)")
result.append(li)
print("result \(result)")
}
}
return result
}
}