diff --git a/Problem-72 Subsets.py b/Problem-72 Subsets.py new file mode 100644 index 00000000..c2e5f3d1 --- /dev/null +++ b/Problem-72 Subsets.py @@ -0,0 +1,26 @@ +# 78. Subsets + +# Time Complexity: O(2^n) +# Space Complexity: O(n) + +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + result = [[]] + if not nums: + return result + + self.helper(nums, 0, [], result) + return result + + def helper(self, nums, pivot, path, result): + # Base + if pivot == len(nums): + return + + # + for i in range(pivot, len(nums)): + path.append(nums[i]) + self.helper(nums, i+1, path, result) + result.append(list(path)) + path.pop() + return diff --git a/Problem-73 Palindrome Partitioning.py b/Problem-73 Palindrome Partitioning.py new file mode 100644 index 00000000..7cbbe1d6 --- /dev/null +++ b/Problem-73 Palindrome Partitioning.py @@ -0,0 +1,32 @@ +# 131. Palindrome Partitioning + +# Time Complexity: O(n*2^n) +# Space Complexity: O(n) + +class Solution: + def partition(self, s: str) -> List[List[str]]: + result = list() + + self.helper(s, 0, list(), result) + return result + + def helper(self, s, idx, path, result): + # Base + if idx == len(s): + result.append(list(path)) + return + + # Logic + for i in range(idx, len(s)): + if self.isPalindrome(s, idx, i): + path.append(s[idx:i+1]) + self.helper(s, i+1, path, result) + path.pop() + + def isPalindrome(self, s, l, r): + while l < r: + if s[l] != s[r]: + return False + l += 1 + r -= 1 + return True