Ad
  • Default User Avatar
  • Custom User Avatar

    Well, then the OP there should be slapped for not telling the actual reason and instead just throwing a complaint without a cause.

  • Default User Avatar

    What troubles you about it?

    it's inefficient on long inputs. ;)

  • Default User Avatar

    The code is short, clear and it works. What troubles you about it?

  • Custom User Avatar

    not stack returns True if stack is empty.

    That should be the case at the end of the function if braces are set correctly.

    In general, it works like this:

    • Iterate over character in the string.
    • If we encounter an opening brace, put the respective closing brace to the top of the stack.
    • If we encounter the closing brace, make sure it's equal to the one at the top of the stack.
    • If that not the case, it means the braces order is incorrect and we should return False.
    • At the end, the stack should be empty (otherwise it means some open brace was not properly closed).