Skip to content
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("{[()()]}") → true
  • balanced("([)]") → false
  • balanced("())") → 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.

brackets.go — go
go · 6 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way