From c878b5249f7df6afee51c033f6a4851b7c70d3b9 Mon Sep 17 00:00:00 2001 From: rishigoswamy Date: Sun, 15 Mar 2026 13:27:17 -0700 Subject: [PATCH] Add implementation for leetcode problems 78, 131 --- leetcode_131.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ leetcode_78.py | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 leetcode_131.py create mode 100644 leetcode_78.py diff --git a/leetcode_131.py b/leetcode_131.py new file mode 100644 index 00000000..64d97216 --- /dev/null +++ b/leetcode_131.py @@ -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 diff --git a/leetcode_78.py b/leetcode_78.py new file mode 100644 index 00000000..310ed678 --- /dev/null +++ b/leetcode_78.py @@ -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