7. Valid parentheses
A string made of the characters (, ), [, ], { and } is balanced when every opening bracket is closed by a bracket of the same kind, and brackets close in the reverse order they were opened.
Write balanced s, which is true exactly when s is balanced.
Examples
- Input
balanced "()"Outputtrue - Input
balanced "()[]{}"Outputtrue - Input
balanced "(]"Outputfalse - Input
balanced "([)]"Outputfalse
Submitting also runs 9 hidden tests.
Constraints
- The empty string is balanced.
- Only the six bracket characters occur in the tests.
Running and submitting solutions, the hints and the editorial come with the Academy Pass. The daily problem is open to every account.
Loading editor…
Type
\to in the editor, or click:Run checks your code against the examples and your custom inputs and records nothing. Submit also runs the hidden tests, confirms the axioms your proof uses and records the result. Each uses one compiler check.
ReadyLn 1, Col 1Lean 4
Draft saved in this browser