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
26 changes: 26 additions & 0 deletions Problem-72 Subsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 78. Subsets

# Time Complexity: O(2^n)
# Space Complexity: O(n)

class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
result = [[]]
if not nums:
return result

self.helper(nums, 0, [], result)
return result

def helper(self, nums, pivot, path, result):
# Base
if pivot == len(nums):
return

#
for i in range(pivot, len(nums)):
path.append(nums[i])
self.helper(nums, i+1, path, result)
result.append(list(path))
path.pop()
return
32 changes: 32 additions & 0 deletions Problem-73 Palindrome Partitioning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 131. Palindrome Partitioning

# Time Complexity: O(n*2^n)
# Space Complexity: O(n)

class Solution:
def partition(self, s: str) -> List[List[str]]:
result = list()

self.helper(s, 0, list(), result)
return result

def helper(self, s, idx, path, result):
# Base
if idx == len(s):
result.append(list(path))
return

# Logic
for i in range(idx, len(s)):
if self.isPalindrome(s, idx, i):
path.append(s[idx:i+1])
self.helper(s, i+1, path, result)
path.pop()

def isPalindrome(self, s, l, r):
while l < r:
if s[l] != s[r]:
return False
l += 1
r -= 1
return True