#C14966. Balanced Brackets

    ID: 44673 Type: Default 1000ms 256MiB

Balanced Brackets

Balanced Brackets

Given a string s consisting of various characters, including three types of brackets: (), {}, and [], determine whether the brackets are balanced.

A string is considered balanced if both of the following conditions are met:

  • For every opening bracket, there exists a corresponding closing bracket of the same type.
  • The pairs of brackets are correctly nested. In other words, if we denote an opening bracket by ch, then its corresponding closing bracket must appear after ch and before any closing bracket for an outer pair.

This can be expressed in latex as: a string is balanced if \( \forall \, \text{opening bracket } b, \exists \, \text{closing bracket } b' \text{ such that } b \text{ matches } b' \) and they are properly nested.

Note that non-bracket characters should be ignored.

For instance:

  • (){}[] is balanced.
  • ([{}]) is balanced.
  • (} is not balanced.

inputFormat

A single line containing the string to be checked. The string may include brackets and other characters.

outputFormat

Print 'True' if the brackets in the string are balanced; otherwise, print 'False'.## sample

(){}[]
True