I am reading the Algorithm Design Manual Second Edition and this is from an exercise question. Quoting the question
A common problem for comp
Remove all non-'(' and -')' characters from an input string. This gives you a string of '(' and ')' only.
If the string has odd length, return false.
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.
Return true.