From f6c7ede0f385ba474dd5e2c1a231543efd648759 Mon Sep 17 00:00:00 2001 From: Kattuvila Milkiyas Date: Sat, 27 Dec 2025 02:58:27 -0800 Subject: [PATCH] 'backtracking-2' --- palindrome-partitioning.py | 78 ++++++++++++++++++++++++++++++++++++++ subsets.py | 60 +++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 palindrome-partitioning.py create mode 100644 subsets.py diff --git a/palindrome-partitioning.py b/palindrome-partitioning.py new file mode 100644 index 00000000..0a2ed7d6 --- /dev/null +++ b/palindrome-partitioning.py @@ -0,0 +1,78 @@ +# without backtrack + +# Run a loop i from pivot to len(s). Check if the substring from pivot to i+1 is a palindrome +# If it is a palindrome, call recursive function with pivot =i+1 and path=path+ss. If pivot is +# out of bounds, append path to result. + +# time: O(n.2^n) +# space: O(n) + +class Solution(object): + + def partition(self, s): + """ + :type s: str + :rtype: List[List[str]] + """ + self.result=[] + self.helper(s,0,[]) + return self.result + + def helper(self, s,pivot,path): + if pivot==len(s): + self.result.append(path[:]) + return + + + for i in range(pivot, len(s)): + ss=s[pivot:i+1] + # print(ss,ss[::-1]) + if ss==ss[::-1]: + self.helper(s,i+1,path+[ss]) + + + + + + + + + + + + + + + +#Using Backtracking + +# Run a loop from pivot to len of s. check if substring of i to pivot is palindrome. +# If it is palindrome, append the sustring to path and call recursive func with pivot=i+1. +# Pop the path for backtracking. If pivot is out of bound, append the path copy to result + +# Time: O(2x2^n) +# Space: O(2^n) +class Solution(object): + + def partition(self, s): + """ + :type s: str + :rtype: List[List[str]] + """ + self.result=[] + self.helper(s,0,[]) + return self.result + + def helper(self, s,pivot,path): + if pivot==len(s): + self.result.append(path[:]) + return + + + for i in range(pivot, len(s)): + ss=s[pivot:i+1] + # print(ss,ss[::-1]) + if ss==ss[::-1]: + path.append(ss) + self.helper(s,i+1,path) + path.pop() \ No newline at end of file diff --git a/subsets.py b/subsets.py new file mode 100644 index 00000000..27799f90 --- /dev/null +++ b/subsets.py @@ -0,0 +1,60 @@ +# Recursive function: +# append the path to result. Run a loop where i=pivot till length of nums. +# Append the elelmt in nums to path and call the helper function by incrementing i. Backtrack the call +# Time :O(2^n) +# Space:O(n) + +class Solution(object): + def subsets(self, nums): + self.result=[] + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + if not nums: + return [] + self.helper(nums, [], 0) + return self.result + + def helper(self, nums, path, pivot): + + self.result.append(path[:]) + for i in range(pivot,len(nums)): + path.append(nums[i]) + self.helper(nums,path,i+1) + path.pop() + + + + + +# Recursive function: +# Check if index is out of bounds, if so, append the path to result., return. +# call the helper function by incrementing the index in case of not choose option. +# Append nums[index] to path and call the helper function by incrementing the index. +# Backtrack the call + +# Time complexity: O(n2^n) +# Space O(n) + +class Solution(object): + def subsets(self, nums): + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + self.result=[] + if not nums: + return [] + else: + self.helper(nums, [], 0) + return self.result + def helper(self, nums, path, ind): + if ind==len(nums): + self.result.append(path[:]) + return + + self.helper(nums, path, ind+1) + path.append(nums[ind]) + self.helper(nums,path,ind+1) + path.pop() \ No newline at end of file