From 3a992b1de1ba552163d356655f1f07069825fc5e Mon Sep 17 00:00:00 2001 From: PrasiddhShah Date: Thu, 6 Nov 2025 12:54:23 -0800 Subject: [PATCH] Backtracking - 2 Completed --- problem1.java | 40 ++++++++++++++++++++++++++++++++ program2.java | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 problem1.java create mode 100644 program2.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..faf53444 --- /dev/null +++ b/problem1.java @@ -0,0 +1,40 @@ +// Time Complexity : O(2^n) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this :yes + +/* + * Approach + * here are we using recursion to solve the problem, + * basically at each step we have to choose between pick or not pick a number + * for this we are using recurion + * + * we are going to use the for loop based recursion and we are using backtrack clean up path + * and we will make a deepcopy of the path when pointer it to the result + * so that we avoid return null result. + */ +class Solution { + 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) { + return; + } + + // logic + result.add(new ArrayList<>(path)); + for (int i = pivot; i < nums.length; i++) { + path.add(nums[i]); + helper(nums, i + 1, path, result); + + path.remove(path.size() - 1); + + } + + } +} \ No newline at end of file diff --git a/program2.java b/program2.java new file mode 100644 index 00000000..f7d17384 --- /dev/null +++ b/program2.java @@ -0,0 +1,64 @@ +// Time Complexity : O(2^n) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this :yes + +/* + * Approach + * here are we using recursion to solve the problem, + * basically at each step we have to choose between pick or not pick a number + * for this we are using recurion + * + * we are going to use the for loop based recursion + * in each recursive call we start a new for loop from pivot till s len + * create a new sub string from pivot to i+1 + * do the palindrome check on the string if true + * add to path make the recursive call again with i+1 + * finally backtrack to keep the path update for the cur level + * + * + * for the palindrome check + * we simply + * loop from both ends of the string to see if the are equal of not + * + */ +class Solution { + 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 cur = s.substring(pivot, i + 1); + if (isPalindrome(cur)) { + // add + path.add(cur); + + // 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; + } + return true; + } +} \ No newline at end of file