From c9298d3b7866d95d6dadb0ec05ff97692f26c7bf Mon Sep 17 00:00:00 2001 From: Manisha Rana Date: Sun, 1 Feb 2026 23:40:44 -0500 Subject: [PATCH] Backtracking-2 Done --- PalindromePartition.java | 47 +++++++++++++++++++++++++++++++++++++++ Subsets.java | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 PalindromePartition.java create mode 100644 Subsets.java diff --git a/PalindromePartition.java b/PalindromePartition.java new file mode 100644 index 00000000..8865c284 --- /dev/null +++ b/PalindromePartition.java @@ -0,0 +1,47 @@ +/* +Time Complexity : O(L.2^L). L is the length of the input string +Space Complexity : O(L), L characters in the recursion stack +Did this code successfully run on Leetcode : yes +Any problem you faced while coding this : No +Approach : + +We need to partition the string, we can do that by creating substring between 2 indices(pivot and index). +We need to check if all the substrings is valid palindrome, if yes, add the substrings to a result array. + +*/ +import java.util.ArrayList; +import java.util.List; + +public class PalindromePartition { + + public List> partition(String s) { + List> result = new ArrayList<>(); + partitionRecursion(s, result, new ArrayList<>(), 0); + return result; + } + + private void partitionRecursion(String s, List> result, List path, int pivot) { + if(pivot == s.length()){ + result.add(new ArrayList<>(path)); + return; + } + + for (int i = pivot; i < s.length(); i++) { + String subString = s.substring(pivot, i+1); + if(isPalindrome(subString)){ + path.add(subString); + partitionRecursion(s, result, path, i+1); + // backtrack + path.remove(path.size()-1); + } + } + } + + boolean isPalindrome(String s){ + int len = s.length(); + for (int i = 0; i < len / 2; i++) { + if(s.charAt(i) != s.charAt(len-1-i)) return false; + } + return true; + } +} diff --git a/Subsets.java b/Subsets.java new file mode 100644 index 00000000..98448a7b --- /dev/null +++ b/Subsets.java @@ -0,0 +1,48 @@ +/* +Time Complexity : O(2^N), nums array size +Space Complexity : O(N), in the recursion stack +Did this code successfully run on Leetcode : Yes +Any problem you faced while coding this : No +Approach : +We need to find all combinations of the numbers in the input array. To find combination, we can either do choose and not choose +recursion, 0/1 recursion or for loop based recursion. In either of the recursion, we need to maintain a path, that would store the current +combination, once we are done visiting a number we can remove it. As in, we need to back track the last value added to the path. +This is because we are using same path object to create all paths, if we want to avoid backtracking, we need to create a new array +for each path. As this is not space efficient, we rather choose to back track. +*/ +import java.util.ArrayList; +import java.util.List; + +public class Subsets { + public List> subsets(int[] nums) { + List> result = new ArrayList<>(); + getSubsets(nums, 0,new ArrayList<>(), result); + // getSubsets01Recursion(nums, 0, new ArrayList<>(), result); + return result; + } + + private void getSubsets01Recursion(int[] nums, int index, List path, List> result) { + if(index == nums.length){ + result.add(new ArrayList<>(path)); + return; + } + + path.add(nums[index]); + getSubsets01Recursion(nums, index+1, path, result); + path.remove(path.size()-1); + + getSubsets01Recursion(nums, index+1, path, result); + } + + // Time Complexity : O(N * 2^N), Extra O(N) as we are creating deep copy of path and in worst case, path can have N values + // Space Complexity : O(N) + private void getSubsets(int[] nums, int pivot, List path, List> result) { + result.add(new ArrayList<>(path)); + + for (int i = pivot; i < nums.length; i++){ + path.add(nums[i]); + getSubsets(nums, i+1, path, result); + path.remove(path.size()-1); + } + } +}