NOI Contest Archive

Archive / 2025 / Qualifier Round

balanced-bracket-string

Medium · NOI 2025 Qualifier Round

You are given a string consisting of the characters (, ), {, }, [, and ] only. Your task is to check if the brackets in the string are balanced. If they are balanced, print YES; otherwise, print NO.

A string of brackets is considered balanced if:

  1. Each opening bracket ((, [, or {) has a corresponding closing bracket (), ], or }) of the same type.
  2. The pairs of brackets are correctly nested — that is, you cannot close a bracket before its matching opening bracket.

Examples of balanced strings

Examples of unbalanced strings

Input Format

The input consists of a single line containing a string of brackets.

Output Format

Print YES if the brackets are balanced, or NO otherwise.

Constraints

Sample Input 0

([]{})

Sample Output 0

YES

Sample Input 1

{[()])

Sample Output 1

NO