diff --git a/Problem1_subsets.py b/Problem1_subsets.py new file mode 100644 index 00000000..9344e61a --- /dev/null +++ b/Problem1_subsets.py @@ -0,0 +1,22 @@ +class Solution(object): + def subsets(self, nums): + """ + Time Complexity: O(N * 2^N) where N is the length of nums + Space Complexity: O(N) for the recursion stack + :type nums: List[int] + :rtype: List[List[int]] + """ + res = [] + + def backtrack(start, path): + #No base case + res.append(path[:]) + + #Logic + for i in range(start, len(nums)): + path.append(nums[i]) + backtrack(i+1, path) + path.pop() + + backtrack(0, []) + return res \ No newline at end of file diff --git a/Problem2_palindrome_partitioning.py b/Problem2_palindrome_partitioning.py new file mode 100644 index 00000000..cc2a7d8b --- /dev/null +++ b/Problem2_palindrome_partitioning.py @@ -0,0 +1,29 @@ +class Solution(object): + def partition(self, s): + """ + Time Complexity: O(N * 2^N) where N is the length of the string s. + Space Complexity: O(N) for the recursion stack and path storage. + :type s: str + :rtype: List[List[str]] + """ + res = [] + + def isPalindrome(substr): + return substr==substr[::-1] + + def backtrack(start, path): + #base case + if start==len(s): + res.append(path[:]) + return + + #logic + for i in range(start+1, len(s)+1): + substr = s[start:i] + if isPalindrome(substr): + path.append(substr) + backtrack(i, path) + path.pop() + + backtrack(0, []) + return res \ No newline at end of file