Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aruku7230/daadb17a8e71c20c6e4bc67be793df18 to your computer and use it in GitHub Desktop.
Save aruku7230/daadb17a8e71c20c6e4bc67be793df18 to your computer and use it in GitHub Desktop.
Algorithm to judge whether two expressions are equivalent

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment