-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathProblem_7_Evaluate_Expression.java
More file actions
43 lines (39 loc) · 1.65 KB
/
Problem_7_Evaluate_Expression.java
File metadata and controls
43 lines (39 loc) · 1.65 KB
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
package Subsets;
// Problem Statement: Evaluate Expression (hard)
// LeetCode Question: 241. Different Ways to Add Parentheses
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Problem_7_Evaluate_Expression {
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
public List<Integer> diffWaysToEvaluateExpression(String input) {
if (map.containsKey(input))
return map.get(input);
List<Integer> result = new ArrayList<>();
// base case: if the input string is a number, parse and return it.
if (!input.contains("+") && !input.contains("-") && !input.contains("*")) {
result.add(Integer.parseInt(input));
} else {
for (int i = 0; i < input.length(); i++) {
char chr = input.charAt(i);
if (!Character.isDigit(chr)) {
List<Integer> leftParts = diffWaysToEvaluateExpression(input.substring(0, i));
List<Integer> rightParts = diffWaysToEvaluateExpression(input.substring(i + 1));
for (int part1 : leftParts) {
for (int part2 : rightParts) {
if (chr == '+')
result.add(part1 + part2);
else if (chr == '-')
result.add(part1 - part2);
else if (chr == '*')
result.add(part1 * part2);
}
}
}
}
}
map.put(input, result);
return result;
}
}