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
36 changes: 36 additions & 0 deletions palindrome-partitioning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Time Complexity: O(N*z^N)
# Space Complexity: O(N)
# Did this code successfully run on Leetcode : Yes

class Solution(object):
def ispal(self,s):
if s==s[::-1]:
return True
return False

def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""

res=[]
path=[]

def helper(pivot):
if pivot == len(s):
res.append(list(path))
return

for i in range(pivot, len(s)):
sub_str=s[pivot:i+1]
if self.ispal(sub_str):
path.append(sub_str)
helper(i+1)
path.pop()

helper(0)
return res



29 changes: 29 additions & 0 deletions subsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Time Complexity: O(N*2^N)
# Space Complexity: O(N)
# Did this code successfully run on Leetcode : Yes

class Solution(object):
def subsets(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res=[]
path=[]

def helper(idx):
#base
if idx==len(nums):
res.append(path[:])
return

#logic
helper(idx+1)

path.append(nums[idx])
helper(idx+1)
path.pop()

helper(0)
return res