From e584bedc0da52615b344a608e122895068f6f055 Mon Sep 17 00:00:00 2001 From: Vaishnavi Gawale Date: Sun, 1 Feb 2026 17:43:59 -0500 Subject: [PATCH 1/2] Backtracking-2 Done --- .DS_Store | Bin 0 -> 6148 bytes palindrome-partitioning.py | 38 ++++++++++++++++++++++++++++++ subsets.py | 47 +++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 .DS_Store create mode 100644 palindrome-partitioning.py create mode 100644 subsets.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[str]]: + self.result = [] + + def helper(s, pivot, path): + #base + if pivot == len(s): + self.result.append(list(path)) + + #logic + for i in range(pivot,len(s)): + sub = s[pivot:i+1] + if self.isPalindrome(sub): + #action + path.append(sub) + #recurse + helper(s,i+1,path) + #backtrack + path.pop() + + helper(s,0,[]) + return self.result + + def isPalindrome(self, sub): + l, r = 0, len(sub)-1 + while l < r: + if sub[l] != sub[r]: + return False + l += 1 + r -= 1 + return True \ No newline at end of file diff --git a/subsets.py b/subsets.py new file mode 100644 index 00000000..3c9c84a8 --- /dev/null +++ b/subsets.py @@ -0,0 +1,47 @@ +#--------Solution 1 : Backtraking------------ +''' Time Complexity : O(n* 2^n)) ; n = len of the list and O(n) to copy path to result + Space Complexity : O(n) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No +''' +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + self.result = [] + + def helper(nums, idx, path): + #base + if idx == len(nums): + self.result.append(list(path)) + return + #logic + #no choose + helper(nums, idx+1, path) + + #choose + path.append(nums[idx]) + helper(nums, idx+1, path) + #backtrack + path.pop() + + helper(nums,0,[]) + return self.result + +#--------Solution 2 : Nested For loop------------ +''' Time Complexity : O(n * n^2)) ; + Space Complexity : O(n) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No +''' +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + result =[[]] + + for i in range(len(nums)): + for j in range(len(result)): + li = result[j].copy() + li.append(nums[i]) + result.append(li) + return result + + + \ No newline at end of file From 7075aed5dfe8bcaef1f20497da6191085e69dc1f Mon Sep 17 00:00:00 2001 From: Vaishnavi Gawale <39532048+vaishnavi2231@users.noreply.github.com> Date: Sun, 1 Feb 2026 17:44:37 -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