From 246c34f0db75fb66807bc474b63ed421be45bb95 Mon Sep 17 00:00:00 2001 From: Param Desai Date: Mon, 29 Sep 2025 09:36:19 -0700 Subject: [PATCH] Backtracking 2 Done --- palindrome.py | 42 ++++++++++++++++++++++++++++++++++++++++++ subset.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 palindrome.py create mode 100644 subset.py diff --git a/palindrome.py b/palindrome.py new file mode 100644 index 00000000..07212573 --- /dev/null +++ b/palindrome.py @@ -0,0 +1,42 @@ +# Time Complexity : O(N.2^N) where N is the size of the list of candidates +# Space Complexity : O(N.2^N) where N is the size of the recursion stack in the worst case +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach: +# I am creating an ans list to store the palindromic partitions. +# I am using a helper function to perform backtracking. +# The helper function takes the current path and the starting index as input. +# If the starting index is equal to the length of the string, I append the current path to the ans list. +# I iterate through the string starting from the starting index to avoid duplicates. +# For each substring, I check if it is a palindrome using the palindrome helper function. +# If it is, I append it to the current path and call the helper function recursively with the updated path and the next index. +# After the recursive call, I remove the last substring from the current path to backtrack. +# Finally, I return the ans list. + +from typing import List +class Solution: + def partition(self, s: str) -> List[List[str]]: + ans = [] + def backtrack(path,idx): + if len(s) == idx: + ans.append(path[:]) + return + for i in range(idx,len(s)): + if palindrome(s,idx,i): + path.append(s[idx:i+1]) + backtrack(path,i+1) + path.pop() + def palindrome(path,start,end): + while start <= end: + if s[start] != s[end]: + return False + start += 1 + end -= 1 + return True + backtrack([],0) + return ans + + + + \ No newline at end of file diff --git a/subset.py b/subset.py new file mode 100644 index 00000000..4a2fb0e3 --- /dev/null +++ b/subset.py @@ -0,0 +1,29 @@ +# Time Complexity : O(N.2^N) where N is the size of the list of nums +# Space Complexity : O(N.2^N) where N is the size of the recursion stack in the worst case +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach: +# I am creating an ans list to store the subsets. +# I am using a helper function to perform backtracking. +# The helper function takes the current path and the starting index as input. +# I append the current path to the ans list. +# I iterate through the nums list starting from the starting index to avoid duplicates. +# For each number, I append it to the current path and call the helper function recursively with the updated path and the next index. +# After the recursive call, I remove the last number from the current path to backtrack. +# Finally, I return the ans list. + +from typing import List +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + ans = [] + def helper(path,idx): + ans.append(path[:]) + for i in range(idx,len(nums)): + path.append(nums[i]) + helper(path,i+1) + path.pop() + helper([],0) + return ans + + \ No newline at end of file