From 5e0a228403070724dd16674f56fc1ebe5768fee8 Mon Sep 17 00:00:00 2001 From: Sejal Jagtap Date: Fri, 31 Oct 2025 08:16:21 -0500 Subject: [PATCH 1/2] Problem 1.py --- Problem 1.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Problem 1.py diff --git a/Problem 1.py b/Problem 1.py new file mode 100644 index 00000000..26241c07 --- /dev/null +++ b/Problem 1.py @@ -0,0 +1,16 @@ +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + res = [] + def helper(i, sub, cursum): + nonlocal res + if cursum > target or i >= len(candidates): + return + if cursum==target: + res.append(sub.copy()) + + for c in range(i, len(candidates)): + sub.append(candidates[c]) + helper(c, sub, cursum+candidates[c]) + sub.pop() + helper(0, [], 0) + return res From 3e6e2b82427abe9a074fa4938d0a179193c15993 Mon Sep 17 00:00:00 2001 From: Sejal Jagtap Date: Fri, 31 Oct 2025 08:16:51 -0500 Subject: [PATCH 2/2] Problem 2.py --- Problem 2.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Problem 2.py diff --git a/Problem 2.py b/Problem 2.py new file mode 100644 index 00000000..78b8c507 --- /dev/null +++ b/Problem 2.py @@ -0,0 +1,24 @@ +class Solution: + def addOperators(self, num: str, target: int) -> List[str]: + n = len(num) + res = [] + def dfs(i,sofar, value, prev): #123 + if i >= n and value == target: + # print(sofar, value) + res.append("".join(sofar)) + return + + for j in range(i, n): + if not sofar: + dfs(j+1, [num[i:j+1]], int(num[i:j+1]), int(num[i:j+1])) + else: + dfs(j+1, sofar + ['+'] + [num[i:j+1]], value+int(num[i:j+1]), int(num[i:j+1]) ) + dfs(j+1, sofar + ['-'] + [num[i:j+1]], value-int(num[i:j+1]), -int(num[i:j+1]) ) + + #for *, bc of bodmas, need to have prev value so that we will add or minus ( opps of last operation), multiply it with current one and then add it back to value(which is prev res or res sofar) + dfs(j+1, sofar + ['*'] + [num[i:j+1]], value-prev + prev*int(num[i:j+1]), prev*int(num[i:j+1])) + + if num[i]=='0': #so that it doesn't consider vals like 00, 05, 005 etc. hence after 1 0, don't dfs more + break + dfs(0, [], 0, None) + return res