Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -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]))
25 changes: 25 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -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