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
78 changes: 78 additions & 0 deletions palindrome-partitioning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# without backtrack

# Run a loop i from pivot to len(s). Check if the substring from pivot to i+1 is a palindrome
# If it is a palindrome, call recursive function with pivot =i+1 and path=path+ss. If pivot is
# out of bounds, append path to result.

# time: O(n.2^n)
# space: O(n)

class Solution(object):

def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
self.result=[]
self.helper(s,0,[])
return self.result

def helper(self, s,pivot,path):
if pivot==len(s):
self.result.append(path[:])
return


for i in range(pivot, len(s)):
ss=s[pivot:i+1]
# print(ss,ss[::-1])
if ss==ss[::-1]:
self.helper(s,i+1,path+[ss])















#Using Backtracking

# Run a loop from pivot to len of s. check if substring of i to pivot is palindrome.
# If it is palindrome, append the sustring to path and call recursive func with pivot=i+1.
# Pop the path for backtracking. If pivot is out of bound, append the path copy to result

# Time: O(2x2^n)
# Space: O(2^n)
class Solution(object):

def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
self.result=[]
self.helper(s,0,[])
return self.result

def helper(self, s,pivot,path):
if pivot==len(s):
self.result.append(path[:])
return


for i in range(pivot, len(s)):
ss=s[pivot:i+1]
# print(ss,ss[::-1])
if ss==ss[::-1]:
path.append(ss)
self.helper(s,i+1,path)
path.pop()
60 changes: 60 additions & 0 deletions subsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Recursive function:
# append the path to result. Run a loop where i=pivot till length of nums.
# Append the elelmt in nums to path and call the helper function by incrementing i. Backtrack the call
# Time :O(2^n)
# Space:O(n)

class Solution(object):
def subsets(self, nums):
self.result=[]
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return []
self.helper(nums, [], 0)
return self.result

def helper(self, nums, path, pivot):

self.result.append(path[:])
for i in range(pivot,len(nums)):
path.append(nums[i])
self.helper(nums,path,i+1)
path.pop()





# Recursive function:
# Check if index is out of bounds, if so, append the path to result., return.
# call the helper function by incrementing the index in case of not choose option.
# Append nums[index] to path and call the helper function by incrementing the index.
# Backtrack the call

# Time complexity: O(n2^n)
# Space O(n)

class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
self.result=[]
if not nums:
return []
else:
self.helper(nums, [], 0)
return self.result
def helper(self, nums, path, ind):
if ind==len(nums):
self.result.append(path[:])
return

self.helper(nums, path, ind+1)
path.append(nums[ind])
self.helper(nums,path,ind+1)
path.pop()