PythonMediumStack & Queue20 min
Validate balanced brackets
Only (), [] and {} appear. Every closing bracket must match the latest unmatched opening bracket; the empty string is valid. Interviewers use this problem to assess use a stack to model last-in-first-out matching.
Input
One line containing brackets.
Output
Print true or false in lowercase.
Constraints
0 <= length <= 100000.
Example
Input
{[()]}Output
true
Example 2 input
()[]{}Output
true
Adjacent balanced groups are valid.
Public test cases (2)
Input
{[()]}Expected output
true
Input
([)]
Expected output
false
Hints (2)
- Identify the state needed to avoid repeated work.
- Use a stack to model last-in-first-out matching.
Target complexity: time O(n) · space O(n)
