Interview question, compare two expressions

  • 0

    I was asked to compare two expressions in the interview.
    For example, -(a+b+c) = -c-a-b or (a-b)-c = -c + a - b

    What is the correct way to tackle this problem.
    I did it through looking for expressions and multiple them with the characters in a map
    for example
    Expression1: a + b -c
    before a, it is null so I consider it as a + and put an entry in hashmap as +a with value is 1. before b, there is a plus so the key is +b and value is 1, before c it is - so key in hash is -c and increment the value
    For next expression

    Expression2: -c + a + b
    I do the same but this time I decrease the count as soon as I encounter the same character/digit

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.