/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
import java.util.*;
public class Main
{
public static void main(String[] args) {
System.out.println(simplifyExpression("(a+b)−(b+c)"));
System.out.println(simplifyExpression("(a−b)−b−b"));
System.out.println(simplifyExpression("a+b−(c+(d−b))"));
}
public static String simplifyExpression(String expression) {
Stack<Integer> operatorStack = new Stack<>();
Stack<Map<Integer, Integer>> operandStack = new Stack<>();
int n = expression.length();
for (int i = 0; i < n; i++) {
if (expression.charAt(i) == '(') {
operatorStack.push((int)expression.charAt(i));
} else if (expression.charAt(i) == ')') {
while (!operatorStack.empty() && ((int)(operatorStack.peek().intValue())) != ((int)'(')) {
int operator = operatorStack.pop();
Map<Integer, Integer> operand2 = operandStack.pop();
Map<Integer, Integer> operand1 = operandStack.pop();
operandStack.push(evaluate(operand1, operand2, (char)operator));
}
operatorStack.pop();
} else if (expression.charAt(i) == '+' || expression.charAt(i) == '−') {
while (!operatorStack.empty() && (((int)(operatorStack.peek().intValue())) != ((int)'('))) {
int operator = operatorStack.pop();
Map<Integer, Integer> operand2 = operandStack.pop();
Map<Integer, Integer> operand1 = operandStack.pop();
operandStack.push(evaluate(operand1, operand2, (char)operator));
}
operatorStack.push((int)expression.charAt(i));
} else {
Map<Integer, Integer> frequencyMap = new HashMap<>();
frequencyMap.put((int)expression.charAt(i), 1);
operandStack.push(frequencyMap);
}
}
while (!operatorStack.empty()) {
int operator = operatorStack.pop();
Map<Integer, Integer> operand2 = operandStack.pop();
Map<Integer, Integer> operand1 = operandStack.pop();
operandStack.push(evaluate(operand1, operand2, (char)operator));
}
StringBuilder result = new StringBuilder();
Map<Integer, Integer> resultMap = operandStack.pop();
for (int val : resultMap.keySet()) {
if (resultMap.get(val).intValue() != 0) {
if (result.length() > 0) {
if (resultMap.get(val).intValue() > 0) {
result.append("+");
} else if (resultMap.get(val).intValue() == -1) {
result.append("-");
}
} else if (resultMap.get(val).intValue() == -1) {
result.append("-");
}
if (Math.abs(resultMap.get(val)) != 1) {
result.append(Integer.toString(resultMap.get(val)));
}
result.append((char)val);
}
}
return result.toString();
}
public static Map<Integer, Integer> evaluate(Map<Integer, Integer> frequencyMap1, Map<Integer, Integer> frequencyMap2, char operator) {
Map<Integer, Integer> result = new HashMap<>();
for (int val : frequencyMap1.keySet()) {
result.put(val, eval(frequencyMap1.get(val), frequencyMap2.getOrDefault(val, 0), operator));
}
for (int val : frequencyMap2.keySet()) {
if (frequencyMap1.containsKey(val)) {
continue;
}
result.put(val, eval(0, frequencyMap2.get(val), operator));
}
return result;
}
public static int eval(int val1, int val2, char operator) {
if (operator == '+') {
return val1 + val2;
} else {
return val1 - val2;
}
}
}