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
63 changes: 63 additions & 0 deletions leetcode_131.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 15 00:00:00 2026

@author: rishigoswamy

LeetCode 131: Palindrome Partitioning
Link: https://leetcode.com/problems/palindrome-partitioning/

Problem:
Given a string s, partition s such that every substring of the partition is a
palindrome. Return all possible palindrome partitioning of s.

Approach:
Backtracking. At each step, try every substring s[pivot:i+1]. If it is a
palindrome, add it to path and recurse from i+1. When pivot reaches the end
of the string, a complete valid partition is found — append a deep copy to results.

1️⃣ Base case: pivot == len(s) → append deep copy of path to results.
2️⃣ Iterate i from pivot to end; extract s[pivot:i+1].
3️⃣ If substring is a palindrome, append to path, recurse with pivot = i+1, pop.
4️⃣ palindrome() uses two pointers to check in O(k) time.

// Time Complexity : O(n * 2^n)
At most 2^n partitions; palindrome check is O(n) per substring.
// Space Complexity : O(n)
Recursive call stack and path are at most O(n) deep.

"""

import copy
from typing import List


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

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

for i in range(pivot, len(s)):
subString = s[pivot:i + 1]
if palindrome(subString):
path.append(subString)
helper(s, i + 1, path)
path.pop(-1)

def palindrome(s):
l = 0
r = len(s) - 1
while l < r:
if s[l] != s[r]:
return False
l += 1
r -= 1
return True

helper(s, 0, [])
return self.result
47 changes: 47 additions & 0 deletions leetcode_78.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 15 00:00:00 2026

@author: rishigoswamy

LeetCode 78: Subsets
Link: https://leetcode.com/problems/subsets/

Problem:
Given an integer array nums of unique elements, return all possible subsets
(the power set). The solution set must not contain duplicate subsets.

Approach:
Backtracking. At each recursive call, the current path represents a valid subset
and is added to results immediately (including the empty set). Then iterate from
pivot onwards to avoid duplicate/reordered subsets, appending each element,
recursing, and backtracking.

1️⃣ Append a copy of path to results at every call (captures all subset sizes).
2️⃣ Iterate i from pivot to end; append nums[i], recurse with pivot = i+1, pop.

// Time Complexity : O(n * 2^n)
2^n subsets, each taking up to O(n) to copy into results.
// Space Complexity : O(n)
Recursive call stack and path are at most O(n) deep.

"""

from typing import List


class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
self.res = []

def helper(nums, pivot, path):
self.res.append(list(path))

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

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