Skip to main content

Warm-up stack · strings

Bracket check

Your editor turns a file red when one bracket never closes. Be that check.

Write isBalanced(s): return true if every bracket in the string is closed by the matching kind, in the right order, and false otherwise. The brackets are (), [] and {}. Any other character is ignored.

Call show(line) after each bracket with the current stack as a string, for example "stack: ( [", and the stage prints the stack growing and shrinking.

Example: isBalanced("{[()]}") returns true, and isBalanced("([)]") returns false.

Tests you can see

  • isBalanced("()[]{}")true
  • isBalanced("(]")false
  • isBalanced("{[()]}")true

More tests are hidden. The server runs them all before a time counts.

Call show() while your code works and the run plays back here.

javascriptisBalanced()

Tab indents · Cmd/Ctrl+Enter runs