diff --git a/palinpartitioning.py b/palinpartitioning.py new file mode 100644 index 00000000..09bc3d63 --- /dev/null +++ b/palinpartitioning.py @@ -0,0 +1,32 @@ +class Solution: + def partition(self, s): + self.result = [] + self.helper(s, []) + return self.result + + def helper(self, s, path): + if len(s) == 0: + self.result.append(list(path)) + return + + for i in range(len(s)): + subStr = s[:i+1] + if self.isPalindrome(subStr): + #action + path.append(subStr) + #recurse + self.helper(s[i+1:], path) + #backtrack + path.pop() + + def isPalindrome(self, s): + left, right = 0, len(s)-1 + while left < right: + if s[left] != s[right]: + return False + left += 1 + right -= 1 + return True + +# TC - O(2^n*n) +# SC - O(n^2) \ No newline at end of file diff --git a/subsets.py b/subsets.py new file mode 100644 index 00000000..d1273cd7 --- /dev/null +++ b/subsets.py @@ -0,0 +1,16 @@ +from typing import List +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + result = [[]] + + for num in nums: + size = len(result) + for j in range(size): + temp = result[j][:] + temp.append(num) + result.append(temp) + + return result + +# TC - O(n*2^n) +# SC - O(n) \ No newline at end of file