FrontendInterviews.dev

Loading problem…

88. Valid Parentheses - Code Formatter/JSON Validator

Easy•
Acceptance: 63.64%
•
🔓3/3 Pro unlocks today

You're building a code editor that needs to validate JSON, HTML, and code structure in real-time. The editor should check if brackets, parentheses, and braces are properly matched and nested.

Given a string containing only the characters '(', ')', '{', '}', '[', and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets
  2. Open brackets must be closed in the correct order
  3. Every close bracket has a corresponding open bracket of the same type

Requirements

1. Basic Validation

  • Check if all opening brackets have matching closing brackets
  • Verify brackets are closed in correct order
  • Return true if valid, false otherwise

Example Usage

isValid("()");     // true
isValid("()[]{}"); // true
isValid("(]");     // false
isValid("([)]");   // false
isValid("{[]}");   // true

Real-World Context

This problem models real code editor features:

  • JSON validator: Validate JSON structure before parsing
  • HTML validator: Check if HTML tags are properly closed
  • Code formatter: Verify bracket matching in code
  • Syntax highlighting: Detect syntax errors in real-time

Constraints

  • 1 <= s.length <= 10^4
  • s consists of parentheses only '()[]{}'
  • Must handle nested brackets correctly
  • Must handle empty string (return true)