Given a string that contains only digits `0-9` and a target value, return all possibilities to add binaryoperators (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: []
``````

Credits:
Special thanks to @davidtan1890 for adding this problem and creating all test cases.

Wrong：[“0+0+0”,”0+0-0”,”0+00”,”0-0+0”,”0-0-0”,”0-00”,”00+0”,”00-0”,”000”,”0+00”,”0-00”,”000”,”00+0”,”00-0”,”000”,”000”]

Correct：[“000”,”00+0”,”00-0”,”0+00”,”0+0+0”,”0+0-0”,”0-00”,”0-0+0”,”0-0-0”]

``````class Solution {
public:
vector<string> addOperators(string num, int target) {
vector<string> res;
helper(num, target, 0, 0, "", res);
return res;
}
void helper(string num, int target, long diff, long curNum, string out, vector<string>& res) {
if (num.size() == 0 && curNum == target) {
res.push_back(out); return;
}
for (int i = 1; i <= num.size(); ++i) {
string cur = num.substr(0, i);
if (cur.size() > 1 && cur[0] == '0') return;
string next = num.substr(i);
if (out.size() > 0) {
helper(next, target, stoll(cur), curNum + stoll(cur), out + "+" + cur, res);
helper(next, target, -stoll(cur), curNum - stoll(cur), out + "-" + cur, res);
helper(next, target, diff * stoll(cur), (curNum - diff) + diff * stoll(cur), out + "*" + cur, res);
} else {
helper(next, target, stoll(cur), stoll(cur), cur, res);
}
}
}
};
``````

Evaluate Reverse Polish Notation

Basic Calculator II

Basic Calculator

Target Sum