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
55 changes: 55 additions & 0 deletions Palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Solution(object):
# tc : O(n*2^n) there are 2^ n calls we make, and at each does the palindrome checks and also deep copy while adding it into the result
# sc : O(n*2) recursive stack and also the substring creation
def partition(self, s):
# intiution
# check if the substr we choosen is a palindrome or not a, palindrome the partion left is ab check if ab is palindrome or not and so on ...
# approach : doing a 0/1 recurssion, where at every index choosin to parition here or skip and parttion the later index
# once done the parition checkl if its palindrome and if it is then we move the partition point one step ahead,we stop once we reach till the end of the strng, i.e once we have processed all the elements in the sting
# then we will add the path , where path is the paritions of the stin g where each partition is a palindrome
"""
:type s: str
:rtype: List[List[str]]
"""
self.result = []
self.n = len(s)
self.helper(0,0,0,s,[])

return self.result


def helper(self, pivot, idx,size, s, path):
# base
if idx == self.n : # check if we are at the end
if size == self.n: # and also making sure , that we have gng through the entire string
self.result.append(list(path)) # making a new path as result will have the refence of the path and as path gets ovrwirtten everytime , deep copying the snapshot at the time and adding its refeence into the result.
return

# not choose
self.helper(pivot,idx+1,size,s,path) # if not choosed to parition here we just move our idx to next pointer

substr = s[pivot:idx+1] # piviot is a start pointer and i is the end pointer of the parition which gives us the substr

if self.isPalindrome(substr):

# action
path.append(substr) # if this parttion is a palindrome then we add out substr into the path and later adding it into the result

# choose
self.helper(idx+1,idx+1, size+len(substr),s, path) # since till idx we have taken for the substr the new paritin will be form the idx+1 ( from the end of the old parition will be ou new pivot)

# backtrack
path.pop() # since the path gets overwritten, before it goes back to the parent cell, we need to undoen the strign we added so we pop it up

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




41 changes: 41 additions & 0 deletions Subsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution(object):
# tc : O(n*2^n) we will have 2^n calls, 1st iteration choose of 1 or not choose 2 options then 4 options and then 8 options and it taks n time for each subset to copy when enterign into the result
# sc : O(n) at once the max space occupied in path would be n
def subsets(self, nums):
# intution
# at every index we have two options take this cell or not , we take 1 we get a subset of 1 , if we choose not to take 2 and then we choose to take 3 we get [1,3]
# so basic idea is to for every cell make combination of choosing that cell or not choosign which will give us the subsets
# will use recurssion, and at every recursive call it goes from the left to right and add the elements in a path varuiable as we travser and decice which elene twe are chosng and add that element in out path varuiable
# once we make a combiantion of all the cells be it choosing it or not choosing i.e hit the end of the arr then we add that path into the final output

self.result = []
self.n = len(nums)
self.helper(0,[],nums)

return self.result


def helper(self,pivot,path,nums):
# base
if pivot == self.n:
self.result.append(list(path)) # since the path gets overwritten ,before adding the path into the result make a deep copy and store it.
return
# lets say the path is of a refernce 420, if we save the path in the result we will have output of [[420], [420], whereas the 420 address gets overwritten so make a deep copy
# like at an address 920 which will be the snapshot of 420 at that point and out final output will be like [[920], [1080].. etc] which gives the proper output.

# not choose case
self.helper(pivot+1,path,nums)


# action
path.append(nums[pivot]) # if we choose this add it into the path

# recruse
# choose case
self.helper(pivot+1, path,nums)

# backtrack
path.pop() # once we have done the left and right recrussion, we go to parent node while gng back our path should not have the nodes we added in this child node,
# so we need to pop it once we go back to our parent node ( in the stack)

return