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
22 changes: 22 additions & 0 deletions Problem1_subsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution(object):
def subsets(self, nums):
"""
Time Complexity: O(N * 2^N) where N is the length of nums
Space Complexity: O(N) for the recursion stack
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []

def backtrack(start, path):
#No base case
res.append(path[:])

#Logic
for i in range(start, len(nums)):
path.append(nums[i])
backtrack(i+1, path)
path.pop()

backtrack(0, [])
return res
29 changes: 29 additions & 0 deletions Problem2_palindrome_partitioning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution(object):
def partition(self, s):
"""
Time Complexity: O(N * 2^N) where N is the length of the string s.
Space Complexity: O(N) for the recursion stack and path storage.
:type s: str
:rtype: List[List[str]]
"""
res = []

def isPalindrome(substr):
return substr==substr[::-1]

def backtrack(start, path):
#base case
if start==len(s):
res.append(path[:])
return

#logic
for i in range(start+1, len(s)+1):
substr = s[start:i]
if isPalindrome(substr):
path.append(substr)
backtrack(i, path)
path.pop()

backtrack(0, [])
return res