← DiffPush

Valid Parentheses

Baseline Stack and Queues · Learning O(n) · O(n)

The Firewall Bracket Audit

A network appliance parses configuration lines made only of round, square, and curly brackets. A line is accepted only if every opening symbol is sealed by its exact matching partner, seals occur in the correct nesting order, and no closer appears without a pending opener. Write the audit routine that accepts or rejects each line.

Input: A string s containing only the characters '(', ')', '{', '}', '[' and ']'.

Output: Return true if the bracket sequence is valid, false otherwise.

Constraints

Examples

Example 1

Input: {"s":"()[]{}"}
Output: true
Each bracket pair is well-formed and the three pairs sit side by side.

Example 2

Input: {"s":"(]"}
Output: false
A round opener cannot be sealed by a square closer.

Solve this in your browser →

Also on LeetCode ↗