Menu
CodeSprout
Typo-tolerant search across every article, problem and post. to open

Valid Parentheses

EasyTime 1000 msMemory 128 MB

You are given a string s containing only the characters (, ), {, }, [ and ].

Decide whether the brackets are balanced. A string is balanced if every opening bracket closes with the matching kind, in the right order.

Input format

A single line: the string s (1 ≤ |s| ≤ 10^5). The string contains only the six bracket characters above and no whitespace.

Output format

A single line: true if s is balanced, false otherwise. (Lowercase, exactly as shown.)

Constraints

  • The empty string is not a valid input (the length is at least 1).
  • A stack of expected closers is the standard O(n) solution.

Example 1

Input
()
Output
true

Explanation: A single pair.

Example 2

Input
()[]{}
Output
true

Explanation: Three balanced pairs in sequence.

Example 3

Input
(]
Output
false

Explanation: The ( expects a ) to close it, not a ].

Example 4

Input
([)]
Output
false

Explanation: Interleaved, not nested — the [ must close before the ( does.

Loading editor…

Input

()

Expected output

true

Run checks all 4 examples; Submit runs the hidden tests.