23. Valid Parentheses

23. Valid Parentheses

Easy
63.0%
2.0k
stringstack
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Oracle

You’re interviewing for a backend or data engineering role at a FAANG-level company. The interviewer describes a real product scenario:

A large platform processes structured text such as configuration files, query expressions, templates, or code snippets. These inputs often contain parentheses, brackets, and braces that must be correctly nested and closed to avoid parsing errors or system failures.

The system represents such an input as a string s consisting only of parentheses characters. Before further processing, the platform needs to verify whether the structure is syntactically valid.

This is a common practice coding problem for DSA question might ask in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Your Task

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

A string is valid if:

  • Open brackets are closed by the same type of brackets
  • Open brackets are closed in the correct order
  • Every closing bracket has a corresponding opening bracket

Input Format

  • s: a string containing only parentheses characters

Output Format

  • A boolean value
    • true if the string is valid
    • false otherwise

Examples

Example 1:

Input: s = "()"
Output: true

Example 2:

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

Example 3:

Input: s = "(]"
Output: false

Constraints

  • 1 <= s.length <= 100,000
  • s consists only of characters ()[]{}

Loading editor...

"()"
true

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback