Exercise · Final project · lesson 3 of 7
Exercise: brackets
Write a function function balanced(s) that checks the brackets in the string s. It returns true if every bracket (, [ or { is later closed by a bracket of the same kind and the pairs do not cross, and false otherwise.
balanced("{[()()]}")→truebalanced("([)]")→falsebalanced("())")→false
Constraints: s holds from 0 to 1000 characters; the function skips every character other than the six brackets; it returns true for an empty string and false for an unclosed or extra bracket.
Do not add extra imports unless the task says so explicitly.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.