From 2b7bfb12a2d7d2bc8e85f48ca5e356b8ccb5e89c Mon Sep 17 00:00:00 2001 From: Srijha-Kalyan <87617310+Srijha-Kalyan@users.noreply.github.com> Date: Mon, 8 Dec 2025 20:47:42 -0500 Subject: [PATCH] Add files via upload --- Problem1_subsets.py | 22 ++++++++++++++++++++++ Problem2_palindrome_partitioning.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 Problem1_subsets.py create mode 100644 Problem2_palindrome_partitioning.py diff --git a/Problem1_subsets.py b/Problem1_subsets.py new file mode 100644 index 00000000..9344e61a --- /dev/null +++ b/Problem1_subsets.py @@ -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 \ No newline at end of file diff --git a/Problem2_palindrome_partitioning.py b/Problem2_palindrome_partitioning.py new file mode 100644 index 00000000..cc2a7d8b --- /dev/null +++ b/Problem2_palindrome_partitioning.py @@ -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 \ No newline at end of file