Different Ways to Add Parentheses

LeetCode Q 241 - Different Ways to Add Parentheses

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 *.

Example 1:
Input: "2-1-1" ; Output: [0, 2]
Explanation:
((2-1)-1) = 0 , (2-(1-1)) = 2

Example 2:
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

Solution : Divide and Conquer

Code:

public List<Integer> diffWaysToCompute(String input) {
	List<Integer> res = new ArrayList<>();
	if (input == null || input.length() == 0) return res;

	for (int i = 0; i < input.length(); i++) {
		if (input.charAt(i) == '+' || input.charAt(i) == '-' || input.charAt(i) == '*') {
			List<Integer> left = diffWaysToCompute(input.substring(0, i));
			List<Integer> right = diffWaysToCompute(input.substring(i + 1));
			for (int num1: left) {
				for (int num2: right) {
					if (s.charAt(i) == '+') res.add(num1 + num2);
					else if (s.charAt(i) == '-') res.add(num1 - num2);
					else if (s.charAt(i) == '+') res.add(num1 * num2); 
				}
			}
		}
	}

	if (res.size() == 0) 
		res.add(Integer.parseInt(input));
	
	return res;
}

   Reprint policy


《Different Ways to Add Parentheses》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC