Ctrl K

Bracket Balance

Easy
You get a string brackets made up only of three kinds of brackets: round (), square [] and curly {}. Check whether the brackets are well formed. That means every opening bracket is matched by a later closing bracket of the same kind, every closing bracket is matched by an earlier opening one, and the pairs nest properly: a bracket opened later must be closed earlier. Return True if the string is well formed and False otherwise.
Example 1
brackets="{[]()}" → True

Inside the curly braces there are two closed pairs, [] and (), and then the curly brace itself is closed.

Example 2
brackets="[(])" → False

The round bracket was opened last, but the square one is closed first, so the nesting is broken.

Example 3
brackets="(()" → False

One of the round brackets is never closed.

Constraints
  • 1 ≤ length of brackets ≤ 10⁵
  • brackets contains only the characters (, ), [, ], {, }
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSame Text After Erasing EasyPeel the Outer Brackets EasyPopping Pairs EasyCase Pairs Cancel Out EasySteps Back to the Top Folder Easy
loading Python…
Input
brackets="{[]()}"
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 150 tests on the server, hidden ones included.