From c448b81bb7069aefecd3c0db4ee6d57121446262 Mon Sep 17 00:00:00 2001 From: Imran Mohammed Date: Thu, 6 Nov 2025 01:00:03 -0800 Subject: [PATCH] Backtracking-2 solutions --- PalindromePartitioning.java | 68 ++++++++++++++++++++++++++++++++++++ PalindromePartitioning2.java | 68 ++++++++++++++++++++++++++++++++++++ Subsets.java | 43 +++++++++++++++++++++++ Subsets2.java | 48 +++++++++++++++++++++++++ Subsets3.java | 35 +++++++++++++++++++ 5 files changed, 262 insertions(+) create mode 100644 PalindromePartitioning.java create mode 100644 PalindromePartitioning2.java create mode 100644 Subsets.java create mode 100644 Subsets2.java create mode 100644 Subsets3.java diff --git a/PalindromePartitioning.java b/PalindromePartitioning.java new file mode 100644 index 00000000..091aa511 --- /dev/null +++ b/PalindromePartitioning.java @@ -0,0 +1,68 @@ +import java.util.ArrayList; +import java.util.List; + +/** + * Palindrome Partitioning + * LeetCode: https://leetcode.com/problems/palindrome-partitioning/ + * + * Approach: + * - Uses backtracking to explore all possible partitions of the input string. + * - At each step, it checks if the current substring is a palindrome. + * - If it is, it adds the substring to the current path and recursively partitions the remaining string. + * - When the end of the string is reached, the current path is added to the result. + * - Backtracking ensures all possible partitions are explored. + * + * Time Complexity: O(N * 2^N) + * - There are 2^(N-1) possible ways to partition a string of length N. + * - For each partition, checking if a substring is a palindrome takes O(N) time in the worst case. + * - So, overall complexity is O(N * 2^N). + * + * Space Complexity: O(N * 2^N) + * - The result list can contain up to 2^(N-1) partitions, each with up to N substrings. + * - The recursion stack can go up to N levels deep. + */ +public class PalindromePartitioning { + + public List> partition(String s) { + List> result = new ArrayList<>(); + helper(s, 0, new ArrayList<>(), result); + return result; + + } + + private void helper(String s, int pivot, List path, List> result) { + //base + if (pivot == s.length()) { + result.add(new ArrayList<>(path)); + return; + } + + //logic + for (int i = pivot; i < s.length(); i++) { + String current = s.substring(pivot, i + 1); + if (isPalindrome(current)) { + //action + path.add(current); + //recurse + helper(s, i + 1, path, result); + //backtrack + path.remove(path.size() - 1); + } + } + } + + private boolean isPalindrome(String s) { + int i = 0; + int j = s.length() - 1; + while (i < j) { + if (s.charAt(i) != s.charAt(j)) return false; + i++; + j--; + } + return true; + } + + public static void main(String[] args) { + System.out.println(new PalindromePartitioning().partition("aabbb")); + } +} diff --git a/PalindromePartitioning2.java b/PalindromePartitioning2.java new file mode 100644 index 00000000..f214b785 --- /dev/null +++ b/PalindromePartitioning2.java @@ -0,0 +1,68 @@ +import java.util.ArrayList; +import java.util.List; + +/** + * Palindrome Partitioning (Variant) + * LeetCode: https://leetcode.com/problems/palindrome-partitioning/ + * + * Approach: + * - Uses backtracking to explore all possible partitions of the input string. + * - Tracks the count of characters included in the current path to ensure full coverage. + * - At each step, checks if the current substring is a palindrome and recursively partitions the remaining string. + * - Backtracking ensures all possible partitions are explored. + * + * Time Complexity: O(N * 2^N) + * - There are 2^(N-1) possible ways to partition a string of length N. + * - For each partition, checking if a substring is a palindrome takes O(N) time in the worst case. + * - So, overall complexity is O(N * 2^N). + * + * Space Complexity: O(N * 2^N) + * - The result list can contain up to 2^(N-1) partitions, each with up to N substrings. + * - The recursion stack can go up to N levels deep. + */ + +public class PalindromePartitioning2 { + + public List> partition(String s) { + List> result = new ArrayList<>(); + helper(s, 0, 0, 0, new ArrayList<>(), result); + return result; + } + + private void helper(String s, int povit, int idx, int count, List path, List> result) { + //base + if (idx == s.length()) { + if (count == s.length()) { + result.add(new ArrayList<>(path)); + } + return; + } + + helper(s, povit, idx + 1, count, path, result); + String current = s.substring(povit, idx+1); + if (isPalindrome(current)) { + count += current.length(); + //action + path.add(current); + //recursion + helper(s, idx + 1, idx + 1, count, path, result); + //backtrack + path.removeLast(); + } + } + + private boolean isPalindrome(String s) { + int i = 0; + int j = s.length() - 1; + while (i < j) { + if (s.charAt(i) != s.charAt(j)) return false; + i++; + j--; + } + return true; + } + + public static void main(String[] args) { + System.out.println(new PalindromePartitioning2().partition("aab")); + } +} diff --git a/Subsets.java b/Subsets.java new file mode 100644 index 00000000..f3c97ecf --- /dev/null +++ b/Subsets.java @@ -0,0 +1,43 @@ +/** + * Subsets (Backtracking) + * LeetCode: https://leetcode.com/problems/subsets/ + * + * Approach: + * - Uses backtracking to generate all possible subsets of the input array. + * - At each step, chooses to include or exclude the current element. + * - Recursively explores both choices and adds each subset to the result. + * + * Time Complexity: O(N * 2^N) + * - There are 2^N possible subsets for an array of length N. + * - Each subset can take up to O(N) time to copy. + * + * Space Complexity: O(N * 2^N) + * - The result list contains 2^N subsets, each with up to N elements. + * - The recursion stack can go up to N levels deep. + */ + +import java.util.ArrayList; +import java.util.List; + +public class Subsets { + + public List> subsets(int[] nums) { + List> result = new ArrayList<>(); + helper(nums, 0, new ArrayList<>(), result); + return result; + } + + private void helper(int[] nums, int idx, List path, List> result) { + //base + if (idx == nums.length) { + result.add(new ArrayList<>(path)); + return; + } + //logic + helper(nums, idx + 1, path, result); + + path.add(nums[idx]); + helper(nums, idx + 1, path, result); + path.removeLast(); + } +} diff --git a/Subsets2.java b/Subsets2.java new file mode 100644 index 00000000..219563b8 --- /dev/null +++ b/Subsets2.java @@ -0,0 +1,48 @@ +import java.util.ArrayList; +import java.util.List; + +/** + * Subsets (Backtracking with Pivot) + * LeetCode: https://leetcode.com/problems/subsets/ + * + * Approach: + * - Uses backtracking and a pivot index to generate all possible subsets. + * - At each recursive call, adds the current path to the result. + * - Iterates from the pivot index, adds each element to the path, and recurses. + * - Backtracks after each recursion to explore all combinations. + * + * Time Complexity: O(N * 2^N) + * - There are 2^N possible subsets for an array of length N. + * - Each subset can take up to O(N) time to copy. + * + * Space Complexity: O(N * 2^N) + * - The result list contains 2^N subsets, each with up to N elements. + * - The recursion stack can go up to N levels deep. + */ + +public class Subsets2 { + + public List> subsets(int[] nums) { + List> result = new ArrayList<>(); + helper(nums, 0, new ArrayList<>(), result); + return result; + } + + private void helper(int[] nums, int pivot, List path, List> result) { + //base + if (pivot == nums.length) { + result.add(new ArrayList<>(path)); + return; + } + result.add(new ArrayList<>(path)); + //logic + for (int i = pivot; i < nums.length; i++) { + //action + path.add(nums[i]); + //recurse + helper(nums, i + 1, path, result); + //backtrack + path.remove(path.size() - 1); + } + } +} diff --git a/Subsets3.java b/Subsets3.java new file mode 100644 index 00000000..b5083727 --- /dev/null +++ b/Subsets3.java @@ -0,0 +1,35 @@ +import java.util.ArrayList; +import java.util.List; + +/** + * Subsets (Iterative) + * LeetCode: https://leetcode.com/problems/subsets/ + * + * Approach: + * - Uses an iterative approach to generate all possible subsets. + * - Starts with an empty subset and iteratively adds each number to existing subsets to form new ones. + * - Efficiently builds the power set without recursion or backtracking. + * + * Time Complexity: O(N * 2^N) + * - There are 2^N possible subsets for an array of length N. + * - Each subset can take up to O(N) time to copy. + * + * Space Complexity: O(N * 2^N) + * - The result list contains 2^N subsets, each with up to N elements. + */ +public class Subsets3 { + + public List> subsets(int[] nums) { + List> result = new ArrayList<>(); + result.add(new ArrayList<>()); + for(int i=0; i list = new ArrayList<>(result.get(j)); + list.add(nums[i]); + result.add(list); + } + } + return result; + } +}