From ce328cb5b3607a977a04ba76efbd035698628faf Mon Sep 17 00:00:00 2001 From: pranjay01 Date: Sat, 28 Mar 2026 16:07:48 -0700 Subject: [PATCH 1/2] Done Backtracking-2 --- Problem1.py | 29 +++++++++++++++++++++++++++++ Problem2.py | 26 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 Problem1.py create mode 100644 Problem2.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..9a87629c --- /dev/null +++ b/Problem1.py @@ -0,0 +1,29 @@ +#Subsets + +from typing import List + + +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + self.result = [] + + self.tmpResult = [] + + self.helper(0, nums) + return self.result + + def helper(self,index, nums): + + if index == len(nums): + self.result.append(self.tmpResult[:]) + return + #no choose + self.helper(index+1, nums) + + #choose + self.tmpResult.append(nums[index]) + self.helper(index+1, nums) + self.tmpResult.pop() + +sol = Solution() +print(sol.subsets([1,2,3])) \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..2c4b1c10 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,26 @@ +class Solution: + def partition(self, s: str) -> List[List[str]]: + result = [] + + def isPalindrome(startIndex, endIndex): + if startIndex >= endIndex: return True + return s[startIndex] == s[endIndex] and isPalindrome(startIndex+1, endIndex-1) + # while left<=right: + # if s[left]!=s[right]: + # return False + # left+=1 + # right-=1 + return True + def helper(pivot, partitionsList): + if pivot == len(s): + result.append(partitionsList[:]) + return + for i in range(pivot, len(s)): + if not isPalindrome(pivot, i): + continue + subString = s[pivot:i+1] + partitionsList.append(subString) + helper(i+1, partitionsList) + partitionsList.pop() + helper(0, []) + return result \ No newline at end of file From cb5b34179f5917ff6a1a9d28f111884ab471bba2 Mon Sep 17 00:00:00 2001 From: pranjay01 Date: Sat, 28 Mar 2026 16:16:36 -0700 Subject: [PATCH 2/2] Done Backtracking-2 --- Problem1.py | 4 ++++ Problem2.py | 11 +++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Problem1.py b/Problem1.py index 9a87629c..db8f25d7 100644 --- a/Problem1.py +++ b/Problem1.py @@ -1,5 +1,9 @@ #Subsets +# use recrussion to create the possible subsets, use tmp result and keep on building on that for every recrussion and once crossed pop it +# Time -> N2^N rrecrusion branching 2 options at step and then building the final result +# Space -> ON the tmpResult and the stack space + from typing import List diff --git a/Problem2.py b/Problem2.py index 2c4b1c10..bd625da5 100644 --- a/Problem2.py +++ b/Problem2.py @@ -1,3 +1,7 @@ +# Apace -> On +# Time NO(2^N) +# Logic -> use helper and pivot to find all the partitions and validate if those are palindrome or not + class Solution: def partition(self, s: str) -> List[List[str]]: result = [] @@ -5,12 +9,7 @@ def partition(self, s: str) -> List[List[str]]: def isPalindrome(startIndex, endIndex): if startIndex >= endIndex: return True return s[startIndex] == s[endIndex] and isPalindrome(startIndex+1, endIndex-1) - # while left<=right: - # if s[left]!=s[right]: - # return False - # left+=1 - # right-=1 - return True + def helper(pivot, partitionsList): if pivot == len(s): result.append(partitionsList[:])