diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..971efdfd --- /dev/null +++ b/Problem1.py @@ -0,0 +1,33 @@ +""" +TC: O(N * 2^N) {where N is the number of elements in `nums`. There are `2^N` possible subsets, and for each one, it takes `O(N)` time to copy it to the result list.} +SC: O(N * 2^N) {The space is determined by the output array, which stores `2^N` subsets, each with a potential length of `N`.} + +Approach: + +This problem is solved using a **backtracking** algorithm. The core idea is to systematically generate all possible subsets by exploring a decision tree where at each step, we consider adding the current element to our path. The recursive `backtrack` function takes a `pivot` index to ensure we only consider elements in increasing order, which prevents duplicate subsets. + +In each recursive call, the current `arr` (representing a subset) is added to our result list. We then iterate from the `pivot` index to the end of the input `nums` array. For each element, we add it to the `arr` and make a recursive call with the next index (`i+1`). After the call returns, we **backtrack** by removing the last element added. This process continues until all possible subsets have been generated. + +The problem ran successfully on LeetCode. +""" +from typing import List +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + + def backtrack(pivot, arr): + #base case + + res.append(arr[:]) + + + #logic + + for i in range(pivot, len(nums)): + arr.append(nums[i]) + backtrack(i+1, arr) + arr.pop() + + + res = [] + backtrack(pivot=0,arr=[]) + return res \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..e5bbc035 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,41 @@ +""" +TC: O(N * 2^N) {where N is the length of the string. In the worst case, there are up to `2^(N-1)` partitions, and each palindrome check takes `O(N)` time.} +SC: O(N) {The space complexity is determined by the recursion stack depth, which can go up to `N`, and the path list.} + +Approach: + +This problem is solved using a **backtracking** algorithm to find all possible palindrome partitions of the input string. The approach is recursive and works by building a valid partition step by step. A helper function `isPal` is used to check if a substring is a palindrome. + +The recursive `backtrack` function iterates through the string, starting from a `pivot` index. For each substring starting at the `pivot`, we check if it's a palindrome. If it is, we add it to our current partition `path` and make a recursive call for the remaining part of the string, with the new `pivot` being the index immediately following the current substring. After the recursive call returns, we **backtrack** by removing the last added substring from our `path`, allowing us to explore alternative partitions. The base case for the recursion is when the `pivot` reaches the end of the string, signifying a complete and valid partitioning. + +The problem ran successfully on LeetCode. +""" +from typing import List +class Solution: + def partition(self, s: str) -> List[List[str]]: + def isPal(char): + l,r = 0, len(char)-1 + while l= len(s): + res.append(path[:]) + return + + #logic + for i in range(pivot, len(s)): + curr = s[pivot:i+1] + if isPal(curr): + path.append(curr) + backtrack(i+1, path) + path.pop() + + res = [] + backtrack(pivot = 0, path = []) + return res \ No newline at end of file