Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +
, -
and *
.
Copy Input: "2-1-1"
Output: [0, 2]
Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2
Copy Input: "2*3-4*5"
Output: [-34, -14, -10, -10, 10]
Explanation:
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
Copy // Top-Down Dynamic Programming with Memoization
vector<int> helper(const string& input, unordered_map<string, vector<int> >& memo) {
if (memo.count(input)) return memo[input];
vector<int> res;
for (int i = 0; i < input.size(); ++i) {
char ch = input[i];
if (!(ch == '+' || ch == '-' || ch == '*')) continue;
string str1 = input.substr(0, i), str2 = input.substr(i + 1);
const vector<int>& res1 = memo.count(str1) ? memo[str1] : helper(str1, memo);
const vector<int>& res2 = memo.count(str2) ? memo[str2] : helper(str2, memo);
for (int n1 : res1) {
for (int n2 : res2) {
switch (ch) {
case '+':
res.emplace_back(n1 + n2);
break;
case '-':
res.emplace_back(n1 - n2);
break;
case '*':
res.emplace_back(n1 * n2);
break;
}
}
}
}
if (res.empty()) res.emplace_back(stoi(input));
return memo[input] = res;
}
vector<int> diffWaysToCompute(string input) {
unordered_map<string, vector<int> > memo;
return helper(input, memo);
}