Easy Text

Valid Parentheses

Receives a string and checks whether its brackets are balanced and correctly nested. The valid pairs are (), [] and {}; any other character (letters, spaces, digits) is ignored. It scans the string left to right with a stack: every opening bracket is pushed, and every closing bracket must match the bracket on top of the stack — if it matches, that opening bracket is popped, otherwise the string is unbalanced. After the scan the string is balanced only if the stack is empty (no opening bracket was left unclosed). Returns true when every bracket is correctly paired and nested, and false otherwise.

Visualization

Input

Algorithm code

Custom input

Saved inputs

References