diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..e2868b51 --- /dev/null +++ b/problem1.py @@ -0,0 +1,20 @@ +# problem 1 +# https://leetcode.com/problems/subsets/ + +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + self.final_arr = [] + self.nums = nums + self.path = [] + self.helper(0) + return self.final_arr + + + def helper(self,i): + if i == len(self.nums): + self.final_arr.append(self.path.copy()) + return + self.helper(i+1) + self.path.append(self.nums[i]) + self.helper(i+1) + self.path.pop() diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..635f7b94 --- /dev/null +++ b/problem2.py @@ -0,0 +1,29 @@ +# problem 2 + +# https://leetcode.com/problems/palindrome-partitioning/ +class Solution: + def partition(self, s: str) -> List[List[str]]: + self.final_arr = [] + self.helper(s,0,[]) + return self.final_arr + + def helper(self,s,pivot,path): + if pivot == len(s): + self.final_arr.append(list(path)) + return + for i in range(pivot,len(s)): + sub_str = s[pivot:i+1] + if self.isPalindrome(sub_str): + path.append(sub_str) + self.helper(s,i+1,path) + path.pop() + + def isPalindrome(self,ip_str): + i = 0 + j = len(ip_str)-1 + while(i