From 76581e0ee0313657c406f96157a311ba3b459961 Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Tue, 19 May 2026 09:08:59 -0700 Subject: [PATCH] Backtracking 2 --- PalindromePartitioning.java | 67 +++++++++++++++++++++++++++++++++++++ Subsets.java | 44 ++++++++++++++++++++++++ 2 files changed, 111 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..a1b221bd --- /dev/null +++ b/PalindromePartitioning.java @@ -0,0 +1,67 @@ +// Time Complexity : O(2^n * 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 +// We will use backtracking to find all the possible combinations of palindromic substrings. +// We will start from the first character and keep adding characters to the current path until we reach the end of the string. +// If the current path is a palindrome, we will add it to the result list and continue to find the next palindrome substring. We will backtrack by removing the last character from the path and continue to find the next palindrome substring. +// We will use a helper function to check if a given substring is a palindrome or not. +// We will use two pointers to compare the characters from the start and end of the substring until they meet in the middle. If any characters do not match, we will return false. If all characters match, we will return true. +// The time complexity of this solution is O(2^n * n) because in the worst case, we will have to explore all possible combinations of substrings, which is 2^n, and for each combination, we will check if it is a palindrome, which takes O(n) time. The space complexity is O(n) because in the worst case, we will have to store all the characters in the path list, which can be at most n characters long. +import java.util.*; + +class Solution { + + List> res; + + public List> partition(String s) { + + res = new ArrayList<>(); + + helper(s, new ArrayList<>(), 0); + + return res; + } + + private void helper(String s, List path, int pivot) { + + if (pivot == s.length()) { + res.add(new ArrayList<>(path)); + return; + } + + for (int i = pivot; i < s.length(); i++) { + + String subStr = s.substring(pivot, i + 1); + + if (isPalindrome(subStr)) { + + path.add(subStr); + + helper(s, path, i + 1); + + path.remove(path.size() - 1); + } + } + } + + private boolean isPalindrome(String s) { + + int left = 0, right = s.length() - 1; + + while (left < right) { + + if (s.charAt(left) != s.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..73e042e6 --- /dev/null +++ b/Subsets.java @@ -0,0 +1,44 @@ +// Time Complexity : O(2^n * n) where n is the number of elements in the input array. This is because we are generating all possible subsets, which is 2^n, and for each subset, we are creating a new list which takes O(n) time in the worst case. +// Space Complexity : O(n) where n is the number of elements in the input array. This is because in the worst case, we will have a subset that contains all the elements of the input array, which will take O(n) space to store. +// 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 will use backtracking to find all the possible subsets of the input array. +// We will start from the first element and keep adding elements to the current path until we reach the end of the array. +// We will add the current path to the result list at each step, and then backtrack by removing the last element from the path and continue to find the next subset. +// The time complexity of this solution is O(2^n * n) because in the worst case, we will have to explore all possible combinations of subsets, which is 2^n, and for each combination, we will create a new list to store the subset, which takes O(n) +import java.util.*; + +class Solution { + + List> res; + + public List> subsets(int[] nums) { + + res = new ArrayList<>(); + + helper(nums, 0, new ArrayList<>()); + + return res; + } + + private void helper(int[] nums, + int indx, + List path) { + + // Add current subset + res.add(new ArrayList<>(path)); + + for (int i = indx; i < nums.length; i++) { + + path.add(nums[i]); + + helper(nums, i + 1, path); + + // backtrack + path.remove(path.size() - 1); + } + } +} \ No newline at end of file