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
31 changes: 31 additions & 0 deletions Palindrome partitioning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution:
def partition(self, s: str) -> List[List[str]]:
self.result=[]
self.helper(s,0,[])
return self.result

def helper(self,s,pivot,path):
#base case
if pivot==len(s):
self.result.append(path[:]) #copy
return
for i in range(pivot,len(s)):
curr_str=s[pivot:i+1]
if self.isPalindrome(curr_str):
#action
path.append(curr_str)
#recurse
self.helper(s,i+1,path)
#backtrack
path.pop()

def isPalindrome(self,s):
l=0
h=len(s)-1

while l<h:
if s[l]!=s[h]:
return False
l=l+1
h=h-1
return True
16 changes: 16 additions & 0 deletions subsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
self.result=[]
self.helper(nums,0,[])
return self.result

def helper(self,nums,i,path):
#base
if i==len(nums):
self.result.append(path)
return

#nochoose
self.helper(nums,i+1,path)
#choose
self.helper(nums,i+1,path+[nums[i]])