Stack · Easy
An expression editor checks delimiter structure before enabling a preview. The caller has already removed all non-bracket text. Your function only checks pairing and nesting; it does not evaluate an expression.
Return whether every opening bracket has a closing bracket of the same type in the correct nested order.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "s": string }.
Fields
- s contains only (, ), [, ], { and }.
Output
Return value
- Return true or false.
- Closing a different type or closing before an opener is invalid.
- All openers must be closed when the input ends. The empty string is valid.
Constraints
- 0 <= s.length <= 100000.
- Quotes, escaping, comments and expression syntax are not part of the input.
Examples
Example 1
{
"s": "([]){}"
}Returns
trueNested and adjacent pairs are allowed.
Example 2
{
"s": "([)]"
}Returns
falseEqual counts do not make crossing pairs valid.
Required complexity
O(n) time and O(n) extra space