Checking string has balanced parentheses

后端 未结 18 2391
谎友^
谎友^ 2020-12-01 08:42

I am reading the Algorithm Design Manual Second Edition and this is from an exercise question. Quoting the question

A common problem for comp

18条回答
  •  星月不相逢
    2020-12-01 08:46

    1. Remove all non-'(' and -')' characters from an input string. This gives you a string of '(' and ')' only.

    2. If the string has odd length, return false.

    3. Else, start reading along our string, adding +1 to a "signature" for each '(' and -1 for each ')'; if this signature is ever negative, return false.

    4. Return true.

提交回复
热议问题