awelgorithms

Reject leftover openings

Updated

If the walk ends and the stack still holds openers, they were never closed. That is invalid. Only an empty stack at the end is balanced (plus no earlier mismatch).

What to focus on

"((" leftover two. "(()" leftover one. "()(" leftover one. Matching is not just equal counts. Leftover means unmatched depth.

Worked examples

two leftover

s = "(("
  1. push push
  2. end with ['(','(']

Result: invalid

one leftover

s = "(()"
  1. push push pop
  2. end with ['(']

Result: invalid

clean

s = "(())"
  1. end []

Result: valid

Try it

Write isValid(s). s is a string of brackets. After the walk, if the stack is nonempty the string is invalid even if no closer mismatched. At the end of "((" leftover two (invalid). "(()" leftover one (invalid). "(())" empty (valid).

s is a string of the same bracket type. No closer mismatches mid-walk. Leftover openers at the end are in scope. That is this skill.