Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions CombinationSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.ArrayList;
import java.util.List;

/*
* Combination Sum - LeetCode 39
* Approach: Backtracking. Try all combinations recursively by including or excluding each candidate.
* Time Complexity: O(2^n * k), where n is the number of candidates and k is the average length of a combination.
* Space Complexity: O(k * x), where x is the number of valid combinations.
* LeetCode Link: https://leetcode.com/problems/combination-sum/
*/

public class CombinationSum {

public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
helper(candidates, target, 0 , new ArrayList<Integer>(), result);
return result;
}

private void helper(int[] candidates, int target, int idx, List<Integer> path, List<List<Integer>> result) {

if (idx == candidates.length || target < 0) return;
//logic
if( target == 0) {
result.add(new ArrayList<>(path));
return;
}

helper(candidates, target, idx+1, path, result);

//action
path.add(candidates[idx]);
//recurse
helper(candidates, target-candidates[idx], idx, path, result);
//backtrack
path.removeLast();

}
}
38 changes: 38 additions & 0 deletions CombinationSum2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.ArrayList;
import java.util.List;

/*
* Combination Sum (Variation) - LeetCode 39
* Approach: Backtracking. Try all combinations recursively, allowing repeated use of elements.
* Time Complexity: O(2^n * k), where n is the number of candidates and k is the average length of a combination.
* Space Complexity: O(k * x), where x is the number of valid combinations.
* LeetCode Link: https://leetcode.com/problems/combination-sum/
*/

public class CombinationSum2 {

public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
helper(candidates, 0, target, new ArrayList<Integer>(), result);
return result;
}

private void helper(int[] candidates, int pivot, int target, List<Integer> path, List<List<Integer>> result) {
if (target < 0) return;

if (target == 0) {
result.add(new ArrayList<>(path));
return;
}

//base
for (int i = pivot; i < candidates.length; i++) {
//action
path.add(candidates[i]);
//recurse
helper(candidates, i, target - candidates[i], path, result);
//backtrack
path.remove(path.size() - 1);
}
}
}
54 changes: 54 additions & 0 deletions ExpressionAddOperators.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Expression Add Operators - LeetCode 282
* Approach: Backtracking. Recursively try all possible operator insertions between digits.
* Time Complexity: O(4^n), where n is the length of the input string.
* Space Complexity: O(n), for recursion stack and path building.
* LeetCode Link: https://leetcode.com/problems/expression-add-operators/
*/

import java.util.ArrayList;
import java.util.List;

public class ExpressionAddOperators {
List<String> result;

public List<String> addOperators(String num, int target) {
result = new ArrayList<>();
helper(num, 0, 0, 0, "", target);
return result;
}

private void helper(String num, int pivot, long currentValue, long tale, String path, int target) {
//base
if (pivot == num.length()) {
if (currentValue == target) {
result.add(path);
}
return;
}

for (int i = pivot; i < num.length(); i++) {
if (i != pivot && num.charAt(pivot) == '0') continue;
long current = Long.parseLong(num.substring(pivot, i + 1));
if (pivot == 0) {
helper(num, i + 1, current, current, path + current, target);
} else {
// + operation
helper(num, i + 1, currentValue + current, current, path + '+' + current, target);

// - operation
helper(num, i + 1, currentValue - current, -current, path + '-' + current, target);

// * operation
helper(num, i + 1, currentValue - tale + current * tale, tale * current, path + '*' + current, target);
}

}

}


public static void main(String[] args) {
System.out.println(new ExpressionAddOperators().addOperators("123", 9));
}
}
66 changes: 66 additions & 0 deletions ExpressionAddOperators2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Expression Add Operators - LeetCode 282
* Approach: Backtracking. Recursively try all possible operator insertions between digits. Uses StringBuilder for efficient path building.
* Time Complexity: O(4^n), where n is the length of the input string.
* Space Complexity: O(n), for recursion stack and path building.
* LeetCode Link: https://leetcode.com/problems/expression-add-operators/
*/

import java.util.ArrayList;
import java.util.List;

public class ExpressionAddOperators2 {
List<String> result;

public List<String> addOperators(String num, int target) {
result = new ArrayList<>();
helper(num, 0, 0, 0, new StringBuilder(), target);
return result;
}

private void helper(String num, int pivot, long currentValue, long tale, StringBuilder path, int target) {
//base
if (pivot == num.length()) {
if (currentValue == target) {
result.add(path.toString());
}
return;
}

for (int i = pivot; i < num.length(); i++) {
if (i != pivot && num.charAt(pivot) == '0') continue;
long current = Long.parseLong(num.substring(pivot, i + 1));
int length = path.length();
if (pivot == 0) {
path.append(current);
helper(num, i + 1, current, current, path, target);
path.setLength(length);
} else {
// + operation
path.append("+");
path.append(current);
helper(num, i + 1, currentValue + current, current, path, target);
path.setLength(length);

// - operation
path.append("-");
path.append(current);
helper(num, i + 1, currentValue - current, -current, path, target);
path.setLength(length);

// * operation
path.append("*");
path.append(current);
helper(num, i + 1, currentValue - tale + current * tale, tale * current, path, target);
path.setLength(length);
}

}

}


public static void main(String[] args) {
System.out.println(new ExpressionAddOperators2().addOperators("123", 9));
}
}