Valid Brackets

arrays · function

Description

Given a string s containing only the characters '(', ')', '{', '}',
'[' and ']', determine if the brackets are properly nested.

A string is valid if:
- every opening bracket is closed by the SAME type of bracket, AND
- brackets are closed in the correct (LIFO) order, AND
- every opening bracket has a matching close (stack is empty at end).

Example:
    s = "{[]}"      -> True
    s = "([)]"      -> False  (interleaved, not nested)
    s = "("         -> False  (unclosed)

Traps:
- Interleaving is NOT nesting. "([)]" has matching counts of each
  bracket type but the close order is wrong.
- Counting opens vs. closes is insufficient -- "(){" has equal opens
  and closes overall but is invalid (also "][" has count parity).
- Don't forget to check the stack is empty at the END. A run that
  never hits a mismatch but leaves opens on the stack (e.g. "(") is
  still invalid.
- Empty string is vacuously valid.

Constraints

- 0 <= len(s) <= 10^4
- s contains only the characters '()[]{}'
solution.py
output
Run the cases to see results here.