Valid Parentheses

easyStackstack of open brackets, match on closeB75Solve on LeetCode#20

Part of Practice: Classic Stacks

Given a string s made only of the six bracket characters ()[]{}, decide whether it is properly balanced. Balanced means every opening bracket is closed by the same type of bracket, in the correct order — each closer matches the most recently opened, still-unclosed bracket.

The string can be up to 10^4 characters. Note that an empty stack of pending brackets at the end is part of being valid: "(" alone is not balanced.

Examples

Input:
s = "()[]{}"
Output:
true

Three pairs, each closed immediately in the right order.

Input:
s = "(]"
Output:
false

The ] tries to close a ( — wrong bracket type.

Input:
s = "([)]"
Output:
false

The ) arrives while [ is the most recent open bracket, so the nesting order is wrong. "([])" would be fine.

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.