Skip to content
BASA DSA

Valid Parentheses

EasyStack

Return whether the bracket string is properly nested and matched. Each closing bracket must match the most recent unmatched opening bracket.

Example 1

Input
s = "{[()]}"
Output
true

Explanation

Closers match the most recent openers in order: (), then [], then {}.

Example 2

Input
s = "([)]"
Output
false

Explanation

The ) arrives while [ is the latest unmatched opener, violating nesting.

Example 3

Input
s = "("
Output
false

Explanation

The opening parenthesis never receives a closer.

Example 4

Input
s = ""
Output
true

Explanation

The empty sequence has no unmatched or misordered brackets.

Constraints

  • 0 <= len(s) <= 10000
  • Characters are ()[]{}

Function contractisValidArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…