The Syntactic Shield
MediumAcc. 91.4%
+30 XP 12
The Perfect Pairing
In programming, ensuring that every opening bracket has a matching closing bracket is critical. Compilers use a Stack to check this: every time they see an opening bracket, they "remember" it; when they see a closing one, they "match" it with the most recent opener.
The Assignment
Your mission is to build a syntactic validator. Your function receives a string s containing only brackets.
- Create an empty stack to track opening brackets:
(, [, {. - Iterate through each character in the string:
- If it is an opening bracket, push it onto the stack.
- If it is a closing bracket, pop from the stack and check if the types match (e.g.,
(matches)). - If they don't match, or the stack is empty when you need to pop, print false and exit.
- After the loop, if the stack is completely empty, print true. Otherwise, print false.
01EXAMPLE 1
Input
s="()[{}]"Output
trueExplanation: All brackets closed correctly.
Constraints
- String only contains () [] {}.
Data StructuresStack
JavaScriptSystem handles I/O — write your function only
Loading...
2 Hidden
Input Arguments
s"()[{}]"
Expected Output
true
Click RUN to test your solution