Exercise · Final project · lesson 3 of 7
Exercise: brackets
Write a function func balanced(s string) bool that checks the brackets in the string s. It returns true if every bracket (, [ or { is closed later by a bracket of the same kind, every closing bracket has a pair, and the pairs do not cross.
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.