From 172f1ac31647f48614e246dcdc37bfce7548b400 Mon Sep 17 00:00:00 2001 From: Sarvani Baru Date: Wed, 11 Mar 2026 14:36:31 -0700 Subject: [PATCH] Done Backtracking-2 --- PalindromePartitioning.java | 45 +++++++++++++++++++++++++++++++++++++ Subsets.java | 32 ++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 PalindromePartitioning.java create mode 100644 Subsets.java diff --git a/PalindromePartitioning.java b/PalindromePartitioning.java new file mode 100644 index 00000000..bcfd14ea --- /dev/null +++ b/PalindromePartitioning.java @@ -0,0 +1,45 @@ +// Time Complexity : O(n * (2 ^ n)) +// Space Complexity : O(n ^ 2) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +We use a for loop based recursion approach where we follow the steps of taking action, recurse and backtrack. +However, the question asks for palindrome partitions. So, we make partitions(by taking substring of pivot and i) + recursively at each i and only want to explore/recurse it further if the substring(first part of partition) + is a palindrome.This way, we prune the invalid paths.We iteratively recurse until our pivot goes out of + bounds and then we add it to our result set. + */ +class Solution { + List> result; + public List> partition(String s) { + this.result = new ArrayList<>(); + helper(s, 0, new ArrayList<>()); + return result; + } + + private void helper(String s, int pivot, List path) { + if(pivot == s.length()) + result.add(new ArrayList<>(path)); + for(int i = pivot ; i < s.length() ; i++) { + String subStr = s.substring(pivot, i + 1); + if(isPalindrome(subStr)) { + path.add(subStr); + helper(s, i + 1, path); + path.remove(path.size() - 1); + } + } + } + + private boolean isPalindrome(String str) { + int left = 0 , right = str.length() - 1; + while(left < right) { + if(str.charAt(left) != str.charAt(right)) + return false; + left++; + right--; + } + return true; + } +} \ No newline at end of file diff --git a/Subsets.java b/Subsets.java new file mode 100644 index 00000000..b9c006d8 --- /dev/null +++ b/Subsets.java @@ -0,0 +1,32 @@ +// Time Complexity : O(n * 2^n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +The idea is to use a for loop based recursion approach where we follow the steps of action, recurse +and backtrack.Our task/action is to add each element to the path and recurse it further without choosing +the same element(pivot) as question said no duplicates.We backtrack at every recursion to find the +possible combinations and not include unrequired/duplicate combination in the result set. + */ +class Solution { + List> result; + public List> subsets(int[] nums) { + this.result = new ArrayList<>(); + helper(nums, 0, new ArrayList<>()); + return result; + } + + private void helper(int[] nums, int pivot, List path) { + result.add(new ArrayList<>(path)); + for(int i = pivot ; i < nums.length ; i++) { + //action + path.add(nums[i]); + //recurse + helper(nums, i + 1, path); + //backtrack + path.remove(path.size() - 1); + } + } +} \ No newline at end of file