From 625dc1ce9697e024aeec049eedb09fd0c0d0bd03 Mon Sep 17 00:00:00 2001 From: Shubham Jain Date: Fri, 30 Jan 2026 17:57:26 -0600 Subject: [PATCH] Completed --- Solutions.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Solutions.py diff --git a/Solutions.py b/Solutions.py new file mode 100644 index 00000000..7ad42701 --- /dev/null +++ b/Solutions.py @@ -0,0 +1,65 @@ +#Problem1 Subsets (https://leetcode.com/problems/subsets/) +# Time Complexity : O(N 2^N) +# Space Complexity : O(N 2^N) + + + +class Solution(object): + def subsets(self, nums): + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + + def __helper(pivot,res,path): + res.append(path[:]) ## append deep copy + + for i in range(pivot,len(nums)): + ## choose + path.append(nums[i]) + ## recurse + __helper(i+1,res,path) + ## no choose + path.pop() + + res = [] + path = [] + __helper(0,res,path) + return res + + +#Problem2 Palindrome Partitioning(https://leetcode.com/problems/palindrome-partitioning/) +# Time Complexity : O(N 2^N) +# Space Complexity : O(N 2^N) + +class Solution(object): + def partition(self, s): + + + # Backtracking approach: + # - Try every possible substring starting from current index (pivot) + # - If the substring is a palindrome, choose it, recurse for the rest + # - Backtrack and explore other options + def isPalindrome(sub): + """Check if a substring is a palindrome.""" + return sub == sub[::-1] + + def __helper(s, pivot, path, result): + + # Base case: reached end of string + if pivot == len(s): + result.append(path[:]) # make a copy of path + return + + # Try all possible substrings starting at pivot + for end in range(pivot, len(s)): + sub = s[pivot:end+1] # substring s[pivot:end] + if isPalindrome(sub): + path.append(sub) # choose + __helper(s, end+1, path, result) # recurse + path.pop() # backtrack + + result = [] + path = [] + __helper(s, 0, path, result) + return result