Quick Overview

Determine whether a long string of parentheses, square brackets, and braces is correctly nested and type-matched. The problem tests stack discipline, early mismatch detection, unmatched openings, and linear processing of large input.

Validate Matching Brackets

Company: ByteDance

Role: Site Reliability Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

# Validate Matching Brackets Given a string containing only the characters `(`, `)`, `[`, `]`, `{`, and `}`, return whether it is valid. A string is valid when every opening bracket is closed by the same bracket type and brackets close in the reverse order in which they were opened. Implement `isValidBrackets(s)`. ## Constraints - `1 <= s.length <= 200,000` - `s` contains only the six bracket characters listed above. - Return a Boolean value. ## Example 1 ```text Input: s = "([]{})" Output: true ``` Every closing bracket matches the most recent unmatched opening bracket. ## Example 2 ```text Input: s = "([)]" Output: false ``` The closing parenthesis appears while the most recent unmatched opening bracket is `[`.

Quick Answer: Determine whether a long string of parentheses, square brackets, and braces is correctly nested and type-matched. The problem tests stack discipline, early mismatch detection, unmatched openings, and linear processing of large input.

Given a nonempty string containing only (, ), [, ], {, and }, return whether it is valid. Every opening bracket must be closed by the same bracket type, and brackets must close in the reverse order in which they were opened.

Constraints

  • 1 <= s.length <= 200,000
  • s contains only (, ), [, ], {, and }.
  • The return value is Boolean.

Examples

Input: ('([]{})',)

Expected Output: True

Explanation: Every closer matches the latest unmatched opener.

Input: ('([)]',)

Expected Output: False

Explanation: The closing parenthesis does not match the latest opener.

Hints

  1. The next closing bracket can match only the most recent unmatched opening bracket.
  2. After the scan, check that no opening bracket remains.

Loading coding console...