public class Solution {
public List<String> addOperators(String num, int target) {
List<String> result = new ArrayList<>();
calculate(result, num, "", target, 0, 0, 0);
return result;
}
public void calculate(List<String> result, String s, String path, int target, long value, int start, long multi) {
if(start == s.length() && value == target) {
result.add(path);
}
for(int i = start; i < s.length(); i++) {
if(i > start && s.charAt(start) == '0') return;
Long curr = Long.valueOf(s.substring(start, i+1));
if(start == 0) {
calculate(result, s, path + curr, target, curr, i+1, curr);
} else {
calculate(result, s, path + "+" + curr, target, value+curr, i+1, curr);
calculate(result, s, path + "-" + curr, target, value-curr, i+1, -curr);
calculate(result, s, path + "*" + curr, target, value-multi+curr*multi, i+1, curr*multi);
}
}
}
}