...

/

Solution Review: Problem Challenge 1

Solution Review: Problem Challenge 1

Evaluate Expression (hard)

Given an expression containing digits and operations (+, -, *), find all possible ways in which the expression can be evaluated by grouping the numbers and operators using parentheses.

Example 1:

Input: "1+2*3"
Output: 7, 9
Explanation: 
  1+(2*3) => 7 
  (1+2)*3 => 9

Example 2:

Input: "2*3-4-5"
Output: 8, -12, 7, -7, -3 
Explanation: 
  2*(3-(4-5)) => 8
  2*(3-4-5) => -12 
  2*3-(4-5) => 7
  2*(3-4)-5 => -7
  (2*3)-4-5 => -3

Solution #

This problem follows the Subsets pattern and can be mapped to Balanced Parentheses. We can follow a similar BFS approach.

Let’s take Example-1 mentioned above to generate different ways to evaluate the expression.

  1. We can iterate through the expression character-by-character.
  2. we can break the expression into two halves whenever we get an operator (+, -, *).
  3. The two parts can be calculated by recursively calling the function.
  4. Once we have the evaluation results from the left and right halves, we can combine them to produce all results.

Code #

Here is what our algorithm will look like:

import java.util.*;
class EvaluateExpression {
public static List<Integer> diffWaysToEvaluateExpression(String input) {
List<Integer> result = new ArrayList<>();
// base case: if the input string is a number, parse and add it to output.
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)) {
// break the equation here into two parts and make recursively calls
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);
}
}
}
}
}
return result;
}
public static void main(String[] args) {
List<Integer> result = EvaluateExpression.diffWaysToEvaluateExpression("1+2*3");
System.out.println("Expression evaluations: " + result);
result = EvaluateExpression.diffWaysToEvaluateExpression("2*3-4-5");
System.out.println("Expression evaluations: " + result);
}
}

Time complexity

The time complexity of this algorithm will be exponential and will be similar to Balanced Parentheses. Estimated time complexity will be ...