From 119091c060dab56709fe218a694c4398e320739a Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Tue, 11 Aug 2026 11:41:47 -0700 Subject: [PATCH] Backtrack --- CombinationSumForBacktrack.java | 43 +++++++++++ CombinationSumRecurseBacktrack.java | 56 ++++++++++++++ Evaluateexp.java | 110 ++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 CombinationSumForBacktrack.java create mode 100644 CombinationSumRecurseBacktrack.java create mode 100644 Evaluateexp.java diff --git a/CombinationSumForBacktrack.java b/CombinationSumForBacktrack.java new file mode 100644 index 00000000..1640702c --- /dev/null +++ b/CombinationSumForBacktrack.java @@ -0,0 +1,43 @@ +import java.util.*; + +class Solution { + + List> res; + + public List> combinationSum(int[] candidates, int target) { + + res = new ArrayList<>(); + + helper(candidates, target, new ArrayList<>(), 0); + + return res; + } + + private void helper(int[] candidates, + int target, + List path, + int indx) { + + if (target < 0) { + return; + } + + if (target == 0) { + res.add(new ArrayList<>(path)); + return; + } + + for (int i = indx; i < candidates.length; i++) { + + path.add(candidates[i]); + + helper(candidates, + target - candidates[i], + path, + i); + + // backtrack + path.remove(path.size() - 1); + } + } +} \ No newline at end of file diff --git a/CombinationSumRecurseBacktrack.java b/CombinationSumRecurseBacktrack.java new file mode 100644 index 00000000..8763efa4 --- /dev/null +++ b/CombinationSumRecurseBacktrack.java @@ -0,0 +1,56 @@ + +// Time Complexity : O(2^n+m) +// Space Complexity : Recursion stack space +// 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 +// this is similar to the coin change problem +// we need to find the combinations of the numbers which adds up to the target +// no need of permutations, only the combinations and add to the res and return it +// we can do normal deep copy, for loop, backtrack anything. +import java.util.*; + +class Solution { + + List> res; + + public List> combinationSum(int[] candidates, int target) { + + res = new ArrayList<>(); + + helper(candidates, target, new ArrayList<>(), 0); + + return res; + } + + private void helper(int[] candidates, + int target, + List path, + int indx) { + + if (target < 0 || indx == candidates.length) { + return; + } + + if (target == 0) { + res.add(new ArrayList<>(path)); + return; + } + + // Skip current number + helper(candidates, target, path, indx + 1); + + // Take current number + path.add(candidates[indx]); + + helper(candidates, + target - candidates[indx], + path, + indx); + + // Backtrack + path.remove(path.size() - 1); + } +} \ No newline at end of file diff --git a/Evaluateexp.java b/Evaluateexp.java new file mode 100644 index 00000000..97ceaf28 --- /dev/null +++ b/Evaluateexp.java @@ -0,0 +1,110 @@ +// Time Complexity :O(4^N) +// Space Complexity : Recursion stack space +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no , just that 2 level tree + + +// Your code here along with comments explaining your approach +// Do 2 level tree recursion . first only for the numbers +// next for the operators +// for each operator do a seperate recursion, when it comes to * and % maintain a tail +// Strin builder + backtracking to avoid new space everytime. +import java.util.*; + +public class Evaluateexp { + List res; + + public List addOperators(String num, int target) { + this.res = new ArrayList<>(); + + helper(num, 0, 0L, new StringBuilder(), 0L, target); + + return res; + } + + private void helper(String num, int pivot, long calc, + StringBuilder path, long tail, int target) { + + if (pivot == num.length()) { + if (calc == target) { + res.add(path.toString()); + } + return; + } + + for (int i = pivot; i < num.length(); i++) { + + // Don't allow numbers with leading zeros + if (num.charAt(pivot) == '0' && i != pivot) { + break; + } + + long curr = Long.parseLong( + num.substring(pivot, i + 1) + ); + + int len = path.length(); + + if (pivot == 0) { + path.append(curr); + + helper( + num, + i + 1, + curr, + path, + curr, + target + ); + + path.setLength(len); + } else { + + // + + path.append("+"); + path.append(curr); + + helper( + num, + i + 1, + calc + curr, + path, + curr, + target + ); + + path.setLength(len); + + // - + path.append("-"); + path.append(curr); + + helper( + num, + i + 1, + calc - curr, + path, + -curr, + target + ); + + path.setLength(len); + + // * + path.append("*"); + path.append(curr); + + helper( + num, + i + 1, + calc - tail + (tail * curr), + path, + tail * curr, + target + ); + + path.setLength(len); + } + } + } +} \ No newline at end of file