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
143 changes: 143 additions & 0 deletions CombinationSum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//
// CombinationSum.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 2/2/26.
//

class CombinationSum {
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
var result = [[Int]]()
helper(candidates: candidates, idx: 0, target: target, path: [], result: &result)
return result
}

//0-1 recursion
/*
time complexity - 2^(m + n)
space complexity - depth of recursion stack - (m+n)
*/
func helper(candidates: [Int], idx: Int, target: Int, path: [Int], result: inout [[Int]]) {
//base
if (target == 0) {
result.append(path)
return
}
if (idx == candidates.count || target < 0) {
return
}

//logic

//recurse

//no choose
helper(candidates: candidates, idx: idx + 1, target: target, path: path, result: &result)
var path = path
path.append(candidates[idx])
//choose
helper(candidates: candidates, idx: idx, target: target - candidates[idx], path: path, result: &result)
}
}

class CombinationSumUsingForLoopBasedRecursion {
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
var result = [[Int]]()
getCombinations(candidates: candidates, start: 0, target: target, path: [], result: &result)
return result
}

//for loop based recursion - time complexty - 2^(m+n)
/*
here width of the tree is more and height is lesser. Effectvely it's choose, not choose algorithm only.
Some places, for loop based recursions is beneficial because it's easier to reach solution.
case in point, permutation of string, combination of string
*/
func getCombinations(candidates: [Int], start: Int, target: Int, path: [Int], result: inout [[Int]]) {
//base
if (target == 0) {
result.append(path)
return
}

if (target < 0) {
return
}

//logic
var path = path
for i in start..<candidates.count {
//action
path.append(candidates[i])

//recurse
getCombinations(candidates: candidates, start: i, target: target - candidates[i], path: path, result: &result)

//backtrack
path.removeLast()
}
}


/*
for loop based recursion - If we want different permutations of the numbers which is summing up to a target then don't pass pivot to the recursive function. Always start from 0.
*/
func getPermutationsWithDifferentArrangements(candidates: [Int], target: Int, path: [Int], result: inout [[Int]]) {
//base
if (target < 0) {
return
}
if (target == 0) {
result.append(path)
return
}

//logic
var path = path
for i in 0..<candidates.count {
//action
path.append(candidates[i])

//recurse
getPermutationsWithDifferentArrangements(candidates: candidates, target: target - candidates[i], path: path, result: &result)

//backtrack
path.removeLast()
}
}
}


//For permutation

class Permutation {
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
var result = [[Int]]()
getCombinationsWithDifferentArrangements(candidates: candidates, target: target, path: [],result: &result)
return result
}

func getCombinationsWithDifferentArrangements(candidates: [Int], target: Int, path: [Int], result: inout [[Int]]) {
//base
if (target < 0) {
return
}
if (target == 0) {
result.append(path)
return
}

//logic
var path = path
for i in 0..<candidates.count {
//action
path.append(candidates[i])

//recurse
getCombinationsWithDifferentArrangements(candidates: candidates, target: target - candidates[i], path: path, result: &result)

//backtrack
path.removeLast()
}
}
}
119 changes: 119 additions & 0 deletions ExpressionAddOperators.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//
// ExpressionAddOperators.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 4/8/26.
//

/*
It has 2 layers of recursion.

+ - *
there are 4 possibilities -
1. Not putting any operator at all.
2. putting +
3. putting -
4. putting *
1234
1 12 123 1234 (outer layer)

*/

class ExpressionAddOperators {
func addOperators(_ num: String, _ target: Int) -> [String] {
var result = [String]()
var path = [Character]()
helper(Array(num), start: 0, target, calculated: 0, tail: 0, path: path, result: &result)
return result
}

func helper(_ numArr: [Character], start: Int, _ target: Int, calculated: Int, tail: Int, path: [Character], result: inout [String]) {
//the start pointer went out of bounds
if (start == numArr.count) {
if (calculated == target) {
result.append(String(path))
}
return
}
//logic
for i in start..<numArr.count {
if (start != i && numArr[start] == "0") {
continue
}
let substr = numArr[start...i]
let currNum = Int(String(substr))! //1, 12, 123, 1234, .... 2, 23, 234....
let strCount = substr.count //used while backtracking

/* at first level, we have only numbers. Fron next levels, we have expressions
*/
if (start == 0) {
// path.append(String(currNum))
helper(numArr, start: i + 1, target, calculated: currNum, tail: currNum, path: path + substr, result: &result) // 23
// path.removeLast(path.count - strCount)
} else {
//how to put all the characters after the substring and append it to the path
//if it't not first level. It's further levels. We have 3 options

//+
helper(numArr, start: i + 1, target, calculated: calculated + currNum, tail: currNum, path: path + "+" + substr, result: &result)

//-
helper(numArr, start: i + 1, target, calculated: calculated - currNum, tail: -currNum, path: path + "-" + substr, result: &result)

//*
helper(numArr, start: i + 1, target, calculated: calculated - tail + tail * currNum, tail: tail * currNum, path: path + "*" + substr, result: &result)
}
}
}

//MARK: using backtracking
func addOperatorsB(_ num: String, _ target: Int) -> [String] {
var result = [String]()
var path: [Character] = []
helperB(Array(num), start: 0, target, calculated: 0, tail: 0, path: &path, result: &result)
return result
}

func helperB(_ numArr: [Character], start: Int, _ target: Int, calculated: Int, tail: Int, path: inout [Character], result: inout [String]) {
//the start pointer went out of bounds
if (start == numArr.count) {
if (calculated == target) {
result.append(String(path))
}
return
}
//logic
for i in start..<numArr.count {
if (start != i && numArr[start] == "0") {
continue
}
let substr = numArr[start...i]
let currNum = Int(String(substr))! //1, 12, 123, 1234, .... 2, 23, 234....
let strCount = substr.count //used while backtracking

/* at first level, we have only numbers. Fron next levels, we have expressions
*/
if (start == 0) {
path += substr
// path.append(Array(substr)
helperB(numArr, start: i + 1, target, calculated: currNum, tail: currNum, path: &path, result: &result) // 23
path.removeLast(strCount)
} else {
//to put all the operators after the substring and append it to the path
//if it't not first level. It's further levels. We have 3 options
//+
path = path + "+" + substr
helperB(numArr, start: i + 1, target, calculated: calculated + currNum, tail: currNum, path: &path, result: &result)
path.removeLast(strCount + 1)
//-
path = path + "-" + substr
helperB(numArr, start: i + 1, target, calculated: calculated - currNum, tail: -currNum, path: &path, result: &result)
path.removeLast(strCount + 1)
//*
path = path + "*" + substr
helperB(numArr, start: i + 1, target, calculated: calculated - tail + tail * currNum, tail: tail * currNum, path: &path, result: &result)
path.removeLast(strCount + 1)
}
}
}
}