From d13796b473c1f472a176259e6fa8232de11df970 Mon Sep 17 00:00:00 2001 From: Vaishnavi Gawale Date: Mon, 9 Feb 2026 14:18:20 -0500 Subject: [PATCH 1/2] Done Backtracking-1 --- .DS_Store | Bin 0 -> 6148 bytes combination-sum.py | 96 +++++++++++++++++++++++++++++++++++++ expression-add-operator.py | 88 ++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 .DS_Store create mode 100644 combination-sum.py create mode 100644 expression-add-operator.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 List[List[int]]: + path = [] + def helper(candidates, i, path, target): + #base + if i >= len(candidates) or target < 0: + return + if target == 0: + self.result.append(list(path)) + return + #logic + #action + path.append(candidates[i]) + #choose + helper(candidates, i, path, target-candidates[i]) + #backtrack + path.pop() + #no Choose + helper(candidates, i+1, path, target) + + helper(candidates,0,path,target) + return self.result + + +#--------Solution 2 : Creating new path each time------------ +''' Time Complexity : O(n*2^(m+n)) + Space Complexity : O(n*h) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No +''' +class Solution: + def __init__(self): + self.result = [] + + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + path = [] + def helper(candidates, i, path, target): + #base + if i >= len(candidates) or target < 0: + return + if target == 0: + self.result.append(list(path)) + return + #logic + #action + new_p = deepcopy(path) + new_p.append(candidates[i]) + #choose + helper(candidates, i, new_p, target-candidates[i]) + #no Choose + helper(candidates, i+1, path, target) + + helper(candidates,0,path,target) + return self.result + +#--------Solution 3 : For loop based recursion with backtracking------------ +''' Time Complexity : O(2^(m+n)) + Space Complexity : O(m+n) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No +''' +class Solution: + def __init__(self): + self.result = [] + + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + path = [] + def helper(candidates, pivot, path, target): + #base + if target < 0: + return + if target == 0: + self.result.append(list(path)) + return + #logic + #action + for i in range(pivot,len(candidates)): + path.append(candidates[i]) + #choose + helper(candidates, i, path, target-candidates[i]) + #backtrack + path.pop() + + + helper(candidates,0,path,target) + return self.result \ No newline at end of file diff --git a/expression-add-operator.py b/expression-add-operator.py new file mode 100644 index 00000000..b3e12a71 --- /dev/null +++ b/expression-add-operator.py @@ -0,0 +1,88 @@ +#--------Solution 1 : FOr loop recursion - creating new path for each calll------------ +''' Time Complexity : O(4^n * n) : n for string concatenation at each call + Space Complexity : O(n) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No +''' + +class Solution: + def addOperators(self, num: str, target: int) -> List[str]: + self.result = [] + self.target = target + + def helper(num, pivot, calc, tail, path): + #base + if pivot == len(num): + if calc == self.target: + self.result.append(path) + #logic + for i in range(pivot, len(num)): + if num[pivot] == '0' and pivot!=i: + break + + curr = int(num[pivot:i+1]) + if pivot == 0: + helper(num, i+1, curr, curr, path + str(curr)) + else: + # for + + helper(num, i+1, calc + curr, curr, path + "+" + str(curr)) + #for - + helper(num, i+1, calc - curr, -curr, path + "-" + str(curr)) + #for * + helper(num, i+1, calc - tail + (tail * curr) , tail * curr, path + "*" + str(curr)) + + helper(num, 0, 0, 0, "") + return self.result + + +#--------Solution 2 : For loop with Backtraking------------ +''' Time Complexity : O(4^n) + Space Complexity : O(n) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No +''' + +class Solution: + def addOperators(self, num: str, target: int) -> List[str]: + self.result = [] + self.target = target + + def helper(num, pivot, calc, tail, path): + #base + if pivot == len(num): + if calc == self.target: + self.result.append("".join(path)) + return + #logic + for i in range(pivot, len(num)): + if num[pivot] == '0' and pivot!=i: + break + + curr = int(num[pivot:i+1]) + le = len(path) + if pivot == 0: + path.append(str(curr)) + helper(num, i+1, curr, curr, path) + path.pop() + else: + # for + + path.append('+') + path.append(str(curr)) + helper(num, i+1, calc + curr, curr, path) + path.pop() + path.pop() + #for - + path.append('-') + path.append(str(curr)) + helper(num, i+1, calc - curr, -curr, path) + path.pop() + path.pop() + #for * + path.append('*') + path.append(str(curr)) + helper(num, i+1, calc - tail + (tail * curr) , tail * curr, path) + path.pop() + path.pop() + + helper(num, 0, 0, 0, []) + return self.result \ No newline at end of file From 18b139483876d871d78c035f698dbc45e3d5952e Mon Sep 17 00:00:00 2001 From: Vaishnavi Gawale <39532048+vaishnavi2231@users.noreply.github.com> Date: Mon, 9 Feb 2026 14:19:20 -0500 Subject: [PATCH 2/2] Delete .DS_Store --- .DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0