Skip to main content
Data Structures & Algorithms·Valid Parentheses
Easy93% accepted

Valid Parentheses

StackString
Problem Statement

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. A string is valid if open brackets are closed by the same type of brackets in the correct order.

Examplespublic test cases
  • Simple valid: () → True

    Input is_valid("()")

    Output True

  • Mixed valid: ()[]{} → True

    Input is_valid("()[]{}")

    Output True

  • Invalid nesting: (] → False

    Input is_valid("(]")

    Output False

  • Nested valid: {[()]} → True

    Input is_valid("{[()]}")

    Output True

Complexity
TimeO(n)
SpaceO(n)
Asked By
GoogleMetaAmazonMicrosoft

Ready to solve?

Open the code editor with the official template, run tests, and iterate.