Validate bracket sequence
IntuitSoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of bracket matching and string parsing, testing competency with basic data structures and algorithmic reasoning such as stack-based matching and correct handling of nested delimiters.
Given a string s containing only the characters (, ), [, ], {, and }, determine whether the brackets are properly matched.
A string is valid if:
- Every opening bracket is closed by a bracket of the same type.
- Brackets are closed in the correct order.
- No closing bracket appears without a matching earlier opening bracket.
Return true if the string is valid and false otherwise.
Example 1:
- Input:
s = "()[]{}" - Output:
true
Example 2:
- Input:
s = "([)]" - Output:
false
Example 3:
- Input:
s = "{[]}" - Output:
true
Be prepared to write and run a few test cases, including edge cases such as the empty string.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.