diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..db8f25d7 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,33 @@ +#Subsets + +# use recrussion to create the possible subsets, use tmp result and keep on building on that for every recrussion and once crossed pop it +# Time -> N2^N rrecrusion branching 2 options at step and then building the final result +# Space -> ON the tmpResult and the stack space + +from typing import List + + +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + self.result = [] + + self.tmpResult = [] + + self.helper(0, nums) + return self.result + + def helper(self,index, nums): + + if index == len(nums): + self.result.append(self.tmpResult[:]) + return + #no choose + self.helper(index+1, nums) + + #choose + self.tmpResult.append(nums[index]) + self.helper(index+1, nums) + self.tmpResult.pop() + +sol = Solution() +print(sol.subsets([1,2,3])) \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..bd625da5 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,25 @@ +# Apace -> On +# Time NO(2^N) +# Logic -> use helper and pivot to find all the partitions and validate if those are palindrome or not + +class Solution: + def partition(self, s: str) -> List[List[str]]: + result = [] + + def isPalindrome(startIndex, endIndex): + if startIndex >= endIndex: return True + return s[startIndex] == s[endIndex] and isPalindrome(startIndex+1, endIndex-1) + + def helper(pivot, partitionsList): + if pivot == len(s): + result.append(partitionsList[:]) + return + for i in range(pivot, len(s)): + if not isPalindrome(pivot, i): + continue + subString = s[pivot:i+1] + partitionsList.append(subString) + helper(i+1, partitionsList) + partitionsList.pop() + helper(0, []) + return result \ No newline at end of file