Determine whether a string of round, square, and curly brackets is properly matched and nested in last-opened, first-closed order, treating the empty string as valid.
## Problem
Given a string containing only the bracket characters `(`, `)`, `[`, `]`, `{`, and `}`, determine whether it is valid.
A string is valid when every opening bracket is closed by the same bracket type and closures occur in last-opened, first-closed order. The empty string is valid.
### Function Contract
Implement `isValidBrackets(text)` and return a Boolean.
### Constraints & Assumptions
- `0 <= len(text) <= 200,000`.
- Every character is one of the six bracket characters listed above.
- A closing bracket cannot match an opening bracket that appears below a still-unclosed opening bracket.
### Clarifying Questions to Ask
- Are non-bracket characters possible? No.
- Is an empty string valid? Yes.
- Must bracket types match as well as counts? Yes.
- Can the function stop as soon as it finds an impossible closing bracket? Yes.
```hint Remember only unmatched openings
When a closing bracket arrives, only the most recent unmatched opening can legally pair with it.
```
### Examples
- `"()[]{}"` returns `true`.
- `"([{}])"` returns `true`.
- `"(]"` returns `false`.
- `"([)]"` returns `false`.
- `"(("` returns `false`.
### Evaluation Focus
- Rejects a mismatched closing type and a closing bracket with no available opener.
- Rejects leftover opening brackets after the scan.
- Runs in `O(n)` time and uses at most `O(n)` auxiliary space.
### Extensions to Discuss
1. How would the contract change if ordinary text could appear between brackets?
2. Can the validator report the index and reason for the first error?
3. Why is a set of counts insufficient for nested structures?
Quick Answer: Determine whether a string of round, square, and curly brackets is properly matched and nested in last-opened, first-closed order, treating the empty string as valid.
Given a string containing only the bracket characters (, ), [, ], {, and }, determine whether it is valid.
A string is valid when every opening bracket is closed by the same bracket type and closures occur in last-opened, first-closed order. The empty string is valid.
Function Contract
Implement isValidBrackets(text) and return a Boolean.
Constraints & Assumptions
0 <= len(text) <= 200,000
.
Every character is one of the six bracket characters listed above.
A closing bracket cannot match an opening bracket that appears below a still-unclosed opening bracket.
Clarifying Questions to Ask Guidance
Are non-bracket characters possible? No.
Is an empty string valid? Yes.
Must bracket types match as well as counts? Yes.
Can the function stop as soon as it finds an impossible closing bracket? Yes.
Examples
"()[]{}"
returns
true
.
"([{}])"
returns
true
.
"(]"
returns
false
.
"([)]"
returns
false
.
"(("
returns
false
.
Evaluation Focus
Rejects a mismatched closing type and a closing bracket with no available opener.
Rejects leftover opening brackets after the scan.
Runs in
O(n)
time and uses at most
O(n)
auxiliary space.
Extensions to Discuss
How would the contract change if ordinary text could appear between brackets?
Can the validator report the index and reason for the first error?
Why is a set of counts insufficient for nested structures?