#K69427. Balanced Square Brackets Checker

    ID: 33084 Type: Default 1000ms 256MiB

Balanced Square Brackets Checker

Balanced Square Brackets Checker

In this problem, you are given a string that may contain square brackets '[' and ']'. Your task is to determine whether the square brackets in the string are balanced. A string is considered balanced if each opening bracket '[' has a corresponding closing bracket ']' and the pairs are correctly nested.

More formally, we define a string s with characters c1, c2, ..., cn to be balanced if the following conditions hold:

( \begin{aligned} &\text{For each } i \text{ where } c_i = '[' \text{ there exists a } j > i \text{ such that } c_j = ']' \ &\text{and the sequence of brackets between } c_i \text{ and } c_j \text{ is also balanced.} \end{aligned} )

Input will be provided from standard input (stdin) and output should be written to standard output (stdout).

Examples:

  • Input: a[b[c]d]e Output: Balanced
  • Input: a[b[c]d Output: Not Balanced

inputFormat

The input consists of a single line containing a string that may include letters, digits, and square brackets '[' and ']'.

outputFormat

Print a single line to standard output. The output should be 'Balanced' if all square brackets in the string are balanced, otherwise print 'Not Balanced'.## sample

a[b[c]d]e
Balanced