-
Notifications
You must be signed in to change notification settings - Fork 154
/
expression-add-operators.js
73 lines (67 loc) · 1.79 KB
/
expression-add-operators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Expression Add Operators
*
* Given a string that contains only digits 0-9 and a target value,
* return all possibilities to add binary operators (not unary) +, -,
* or * between the digits so they evaluate to the target value.
*
* Example 1:
*
* Input: num = "123", target = 6
* Output: ["1+2+3", "1*2*3"]
*
* Example 2:
*
* Input: num = "232", target = 8
* Output: ["2*3+2", "2+3*2"]
*
* Example 3:
*
* Input: num = "105", target = 5
* Output: ["1*0+5","10-5"]
*
* Example 4:
*
* Input: num = "00", target = 0
* Output: ["0+0", "0-0", "0*0"]
*
* Example 5:
*
* Input: num = "3456237490", target = 9191
* Output: []
*/
/**
* @param {string} num
* @param {number} target
* @return {string[]}
*/
const addOperators = (num, target) => {
const result = [];
backtracking(num, target, 0, 0, 0, '', result);
return result;
};
const backtracking = (num, target, start, total, last, solution, result) => {
if (start === num.length) {
if (total === target) {
result.push(solution);
}
return;
}
for (let i = start; i < num.length; i++) {
// for example, input is "105", we don't need answer like "1*05"
if (i > start && num[start] === '0') {
break;
}
const curr = parseInt(num.substring(start, i + 1));
if (start === 0) {
// this is the first number
backtracking(num, target, i + 1, total + curr, curr, solution + curr, result);
} else {
// not the first number, let's add operators
backtracking(num, target, i + 1, total + curr, curr, solution + '+' + curr, result);
backtracking(num, target, i + 1, total - curr, -curr, solution + '-' + curr, result);
backtracking(num, target, i + 1, total - last + last * curr, last * curr, solution + '*' + curr, result);
}
}
};
export { addOperators };