From 56365edf8e4c49653af5158e2550bf58f718725c Mon Sep 17 00:00:00 2001 From: Sahithipsl470 Date: Sat, 20 Dec 2025 23:44:19 -0500 Subject: [PATCH] "Backtracking-2 done" --- Problem-1.java | 23 +++++++++++++++++++++++ Problem-1.py | 19 +++++++++++++++++++ Problem-2.java | 41 +++++++++++++++++++++++++++++++++++++++++ Problem-2.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 Problem-1.java create mode 100644 Problem-1.py create mode 100644 Problem-2.java create mode 100644 Problem-2.py diff --git a/Problem-1.java b/Problem-1.java new file mode 100644 index 00000000..9f507637 --- /dev/null +++ b/Problem-1.java @@ -0,0 +1,23 @@ +// Time Complexity: O(n * 2^n), n = length of input array, because each element can double the number of subsets +// Space Complexity: O(2^n), to store all subsets +// 1. Start with an empty subset and iteratively build all subsets by adding each number to existing subsets. +// 2. For each number, copy all existing subsets, add the current number, and append to the result. +// 3. At the end, the result contains all possible subsets of the input array. + +class Solution { + public List> subsets(int[] nums) { + List> result = new ArrayList<>(); + result.add(new ArrayList<>()); // start with empty subset + + for (int num : nums) { + int resultSize = result.size(); + for (int j = 0; j < resultSize; j++) { + List temp = new ArrayList<>(result.get(j)); // make a copy + temp.add(num); // add current number + result.add(temp); + } + } + + return result; + } +} \ No newline at end of file diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..296a6d19 --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,19 @@ +# Time Complexity: O(n * 2^n), n = length of input array, because each element can double the number of subsets +# Space Complexity: O(2^n), to store all subsets +# 1. Start with an empty subset and iteratively build all subsets by adding each number to existing subsets. +# 2. For each number, copy all existing subsets, add the current number, and append to the result. +# 3. At the end, the result contains all possible subsets of the input array. + +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + result = [] + result.append([]) + for i in range(len(nums)): + result_size = len(result) + for j in range(result_size): + temp = result[j].copy() + temp.append(nums[i]) + result.append(temp) + print(result) + return result + \ No newline at end of file diff --git a/Problem-2.java b/Problem-2.java new file mode 100644 index 00000000..a8d38bf5 --- /dev/null +++ b/Problem-2.java @@ -0,0 +1,41 @@ +// Time Complexity: O(n * 2^n) in the worst case, n = length of the string +// Space Complexity: O(n) for recursion stack + O(total partitions) for result +// 1. Use DFS/backtracking to explore all possible partitions of the string. +// 2. For each substring, check if it is a palindrome before adding to the current path. +// 3. Add the current path to the result when the end of the string is reached and backtrack. + +class Solution { + List> result; + + public List> partition(String s) { + result = new ArrayList<>(); + helper(s, new ArrayList<>(), 0); + return result; + } + + private void helper(String s, List path, int pivot) { + if (pivot == s.length()) { + result.add(new ArrayList<>(path)); + return; + } + + for (int i = pivot; i < s.length(); i++) { + String curr = s.substring(pivot, i + 1); + if (isPalindrome(curr)) { + path.add(curr); + helper(s, path, i + 1); + path.remove(path.size() - 1); // backtrack + } + } + } + + private boolean isPalindrome(String curr) { + int i = 0, j = curr.length() - 1; + while (i < j) { + if (curr.charAt(i) != curr.charAt(j)) return false; + i++; + j--; + } + return true; + } +} diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..ae0bf260 --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,31 @@ +# Time Complexity: O(n * 2^n) in the worst case, n = length of the string +# Space Complexity: O(n) for recursion stack + O(total partitions) for result +# 1. Use DFS/backtracking to explore all possible partitions of the string. +# 2. For each substring, check if it is a palindrome before adding to the current path. +# 3. Add the current path to the result when the end of the string is reached and backtrack. + +class Solution: + def partition(self, s: str) -> List[List[str]]: + self.result = [] + self.helper(s,[],0) + return self.result + def helper(self,s,path,pivot): + if pivot == len(s): + self.result.append(list(path)) + return + for i in range(pivot, len(s)): + curr = s[pivot:i+1] + if self.isPalindrome(curr): + path.append(curr) + self.helper(s,path,i+1) + path.pop() + def isPalindrome(self,curr): + i = 0 + j = len(curr)-1 + while i < j: + if curr[i] != curr[j]: + return False + i += 1 + j -= 1 + return True +