Given two arithmetic expressions e1 and e2 that have same four operands, judge if they are equivalent. Two expression are equivalent if they can be arranged to be the same expression according to mathematical properties. Return true if they are equivalent, otherwise false.
I just cannot figure out an algorithm to solve this problem. I need this algorithm to solve the problem to list all solutions without replication of 24 Game.
Examples
Example 1:
- Input: e1 is "6 * (2 + 5 - 3)", e2 is "(5 - 3 + 2) * 6".
- Output: true
Example 2:
- Input: e1 is "5 - (2 - 7 * 3)", e2 is "3 * 7 + 5 - 3".
- Output: true
Example 3:
- Input: e1 is "(7 + 5) / (2 / 4)", e2 is "(7 + 5) * 4 / 2".
- Output: true
Example 4:
- Input: e1 is "(7 + 5) * 4 / 2", e2 is "(7 + 5) * (4 - 2)".
- Output: false.
Constraints
- e1 and e2 have exactly the same four integer number as operands that is between 1 to 10.
- Only four basic binary arithmetic operation can be used:
addition(+), subtraction(-), multiplication(*), division(/). - Minus(-) can only be used as a subtraction operator and not as a unary operator to compute the numeric negation of its operand.
- Parentheses can be used.