From 88d7546ab5e15d8e05fe0dc94da7411d79571c93 Mon Sep 17 00:00:00 2001 From: tmujje Date: Thu, 27 May 2021 01:16:09 -0400 Subject: [PATCH 1/3] Backtracking-1 Submission --- CombinationSum.java | 39 ++++++++++++++++++++++++++++++ ExpressionOperators.java | 51 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 CombinationSum.java create mode 100644 ExpressionOperators.java diff --git a/CombinationSum.java b/CombinationSum.java new file mode 100644 index 00000000..ef98d668 --- /dev/null +++ b/CombinationSum.java @@ -0,0 +1,39 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(2powern) since we have two choices (choose or not choose) that are being selected exponentially +//Space Complexity: O(n) where n is the height of the choices tree +class Solution { + List> result; + public List> combinationSum(int[] candidates, int target) + { + result = new ArrayList<>(); + if(candidates == null || candidates.length == 0) return result; + backtrack(candidates, target, 0, new ArrayList<>()); + return result; + } + + private void backtrack(int[] candidates, int target, int i, List path){ + + //base + if(target == 0) { + result.add(new ArrayList<>(path)); + return; + } + + if(target < 0 || i == candidates.length){ + return; + } + + //case 1 - Not choose + backtrack(candidates, target, i+1, path); + + //case 2 - Choose + // once the not choose backtrack call ends, control goes to the right most child + path.add(candidates[i]); + backtrack(candidates, target - candidates[i], i, path); + + //backtrack + path.remove(path.size() - 1); // remove last element added + } +} \ No newline at end of file diff --git a/ExpressionOperators.java b/ExpressionOperators.java new file mode 100644 index 00000000..69cec777 --- /dev/null +++ b/ExpressionOperators.java @@ -0,0 +1,51 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(4 power n) since we have 4 choices (no operator, +, -, *) being choosen exponentially +//Space Complexity: O(H) where H is the height of the choices tree + class Solution { + List result; + public List addOperators(String num, int target) { + result = new ArrayList<>(); + if(num == null || num.length() == 0) return result; + helper(num, target, "", 0, 0, 0); + return result; + } + + private void helper(String num, int target, String path, long calc, long tail, int index) + { + //base case + if(index == num.length()){ + if(target == calc){ + result.add(path); + return; + } + } + + //logic + for(int i = index; i < num.length(); i++) + { + // Handle 0 digit being ignored + if(num.charAt(index) =='0' && index != i) + { + continue; + } + + long curr = Long.parseLong(num.substring(index, i+1)); + if(index == 0){ + helper(num, target, path + curr, curr, curr, i+1); + } + else + { + // + case + helper(num, target, path + "+" + curr, calc + curr, curr, i+1); + + // - case + helper(num, target, path + "-" + curr, calc - curr, -curr, i+1); + + // * case + helper(num, target, path + "*" + curr, calc - tail + tail * curr, tail * curr, i+1); + } + } + } +} \ No newline at end of file From 6f05c608f4111d7d46b7d5b6ba9f0b3e3b624427 Mon Sep 17 00:00:00 2001 From: TejBharath Mujje Date: Tue, 1 Sep 2026 00:04:32 -0400 Subject: [PATCH 2/3] Add combination sum solution --- CombinationSum.java | 39 --------------------------- CombinationSumBackTrack.java | 38 +++++++++++++++++++++++++++ ExpressionOperators.java | 51 ------------------------------------ 3 files changed, 38 insertions(+), 90 deletions(-) delete mode 100644 CombinationSum.java create mode 100644 CombinationSumBackTrack.java delete mode 100644 ExpressionOperators.java diff --git a/CombinationSum.java b/CombinationSum.java deleted file mode 100644 index ef98d668..00000000 --- a/CombinationSum.java +++ /dev/null @@ -1,39 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(2powern) since we have two choices (choose or not choose) that are being selected exponentially -//Space Complexity: O(n) where n is the height of the choices tree -class Solution { - List> result; - public List> combinationSum(int[] candidates, int target) - { - result = new ArrayList<>(); - if(candidates == null || candidates.length == 0) return result; - backtrack(candidates, target, 0, new ArrayList<>()); - return result; - } - - private void backtrack(int[] candidates, int target, int i, List path){ - - //base - if(target == 0) { - result.add(new ArrayList<>(path)); - return; - } - - if(target < 0 || i == candidates.length){ - return; - } - - //case 1 - Not choose - backtrack(candidates, target, i+1, path); - - //case 2 - Choose - // once the not choose backtrack call ends, control goes to the right most child - path.add(candidates[i]); - backtrack(candidates, target - candidates[i], i, path); - - //backtrack - path.remove(path.size() - 1); // remove last element added - } -} \ No newline at end of file diff --git a/CombinationSumBackTrack.java b/CombinationSumBackTrack.java new file mode 100644 index 00000000..9a9e7809 --- /dev/null +++ b/CombinationSumBackTrack.java @@ -0,0 +1,38 @@ +//Time Complexity: 2 power (m+n) +//Space Compexity: O(m+n) + O(n) +//For primitive data type in result instead of exact path (list of integers etc.) we can use DP else use Backtracking for optimal solution + +//Back Tracking +class Solution { + List> result; + public List> combinationSum(int[] candidates, int target) { + this.result = new ArrayList<>(); + if(candidates == null || candidates.length == 0) return result; + helper(candidates, 0, target, new ArrayList<>()); + return result; + } + + private void helper(int[] candidates, int i, int target, List path) + { + //Base case + if(target < 0 || i == candidates.length){ + return; + } + + if(target == 0) + { + result.add(new ArrayList<>(path)); + return; + } + + //case 0 + helper(candidates, i+1, target, path); + + //case 1 + path.add(candidates[i]); + helper(candidates, i, target-candidates[i], path); + + //backtrack + path.remove(path.size() - 1); // remove last element added + } +} \ No newline at end of file diff --git a/ExpressionOperators.java b/ExpressionOperators.java deleted file mode 100644 index 69cec777..00000000 --- a/ExpressionOperators.java +++ /dev/null @@ -1,51 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(4 power n) since we have 4 choices (no operator, +, -, *) being choosen exponentially -//Space Complexity: O(H) where H is the height of the choices tree - class Solution { - List result; - public List addOperators(String num, int target) { - result = new ArrayList<>(); - if(num == null || num.length() == 0) return result; - helper(num, target, "", 0, 0, 0); - return result; - } - - private void helper(String num, int target, String path, long calc, long tail, int index) - { - //base case - if(index == num.length()){ - if(target == calc){ - result.add(path); - return; - } - } - - //logic - for(int i = index; i < num.length(); i++) - { - // Handle 0 digit being ignored - if(num.charAt(index) =='0' && index != i) - { - continue; - } - - long curr = Long.parseLong(num.substring(index, i+1)); - if(index == 0){ - helper(num, target, path + curr, curr, curr, i+1); - } - else - { - // + case - helper(num, target, path + "+" + curr, calc + curr, curr, i+1); - - // - case - helper(num, target, path + "-" + curr, calc - curr, -curr, i+1); - - // * case - helper(num, target, path + "*" + curr, calc - tail + tail * curr, tail * curr, i+1); - } - } - } -} \ No newline at end of file From 840d13308a6160d05eb932f87708327d9909067c Mon Sep 17 00:00:00 2001 From: TejBharath Mujje Date: Thu, 3 Sep 2026 00:05:22 -0400 Subject: [PATCH 3/3] Add ExpressionAddOperators --- ExpressionAddOperators.java | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 ExpressionAddOperators.java diff --git a/ExpressionAddOperators.java b/ExpressionAddOperators.java new file mode 100644 index 00000000..07924e1b --- /dev/null +++ b/ExpressionAddOperators.java @@ -0,0 +1,46 @@ +//Time Complexity : O(4 power n) +//Space Complexity: O(h) +class Solution { + List result; + public List addOperators(String num, int target) { + result = new ArrayList<>(); + if(num == null || num.length() == 0) return result; + helper(num, target, "", 0, 0, 0); + return result; + } + + private void helper(String num, int target, String path, long calc, long tail, int index) + { + //base case + if(index == num.length()){ + if(target == calc){ + result.add(path); + return; + } + } + //logic + + for(int i = index; i < num.length(); i++) + { + if(num.charAt(index) =='0' && index != i) + { + continue; + } + long curr = Long.parseLong(num.substring(index, i+1)); + if(index == 0){ + helper(num, target, path + curr, curr, curr, i+1); + } + else + { + // + case + helper(num, target, path + "+" + curr, calc + curr, curr, i+1); + + // - case + helper(num, target, path + "-" + curr, calc - curr, -curr, i+1); + + // * case + helper(num, target, path + "*" + curr, calc - tail + tail * curr, tail * curr, i+1); + } + } + } +} \ No newline at end of file