Stack · Easy
A small editor checks bracket structure before offering an expression preview. Input contains only delimiters, not JavaScript source.
Return whether (), [] and {} are correctly paired and nested.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ text: string }. text contains only the six bracket characters
What each field means
text— a string containing only(,),[,],{and}.
Output
Return value and rules
- Return
trueonly if every opening bracket has the correct closing bracket and nesting order. - Return
falsefor an unmatched bracket or incorrect nesting. - The empty string returns
true.
Constraints
- 0 <= text.length <= 100000
- Do not parse quotes, comments or HTML; those are outside this contract.
Examples
Example 1
{
"text": "([]){}"
}Returns
trueExample 2
{
"text": "([)]"
}Returns
falseRequired complexity
O(n) time and O(n) space