Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added task 241.
  • Loading branch information
javadev committed Nov 23, 2021
commit c6332892ccfdcc71da4bc2f60b8d3cac91804874
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package g0201_0300.s0241_different_ways_to_add_parentheses;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
public List<Integer> diffWaysToCompute(String expression) {
return diffWaysToCompute(expression, new HashMap<>());
}

private List<Integer> diffWaysToCompute(String expression, Map<String, List<Integer>> map) {
if (map.containsKey(expression)) {
return map.get(expression);
}
List<Integer> values = new ArrayList<>();
if (!hasOperator(expression)) {
// base case
values.add(Integer.parseInt(expression));
} else {
// Recursive case. DFS
for (int i = 0; i < expression.length(); i++) {
char symbol = expression.charAt(i);

if (!Character.isDigit(symbol)) {
List<Integer> left = diffWaysToCompute(expression.substring(0, i), map);
List<Integer> right = diffWaysToCompute(expression.substring(i + 1), map);
for (Integer l : left) {
for (Integer r : right) {
switch (symbol) {
case '+':
values.add(l + r);
break;
case '-':
values.add(l - r);
break;
case '*':
values.add(l * r);
break;
}
}
}
}
}
}
map.put(expression, values);
return values;
}

private boolean hasOperator(String expression) {
for (int i = 0; i < expression.length(); i++)
switch (expression.charAt(i)) {
case '+':
case '-':
case '*':
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
241\. Different Ways to Add Parentheses

Medium

Given a string `expression` of numbers and operators, return _all possible results from computing all the different possible ways to group numbers and operators_. You may return the answer in **any order**.

**Example 1:**

**Input:** expression = "2-1-1"

**Output:** \[0,2\]

**Explanation:** ((2-1)-1) = 0 (2-(1-1)) = 2

**Example 2:**

**Input:** expression = "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

**Constraints:**

* `1 <= expression.length <= 20`
* `expression` consists of digits and the operator `'+'`, `'-'`, and `'*'`.
* All the integer values in the input expression are in the range `[0, 99]`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g0201_0300.s0241_different_ways_to_add_parentheses;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Arrays;
import org.junit.Test;

public class SolutionTest {
@Test
public void diffWaysToCompute() {
assertThat(new Solution().diffWaysToCompute("2-1-1"), equalTo(Arrays.asList(2, 0)));
}

@Test
public void diffWaysToCompute2() {
assertThat(
new Solution().diffWaysToCompute("2*3-4*5"),
equalTo(Arrays.asList(-34, -10, -14, -10, 10)));
}
}