FastPrepValid Parentheses

Valid Parentheses

Meta logoMeta● EasyINTERNPHONE SCREEN
Learn

Problem statement

Given a nonempty string s containing only (, ), [, ], {, and }, determine whether it is valid.

A string is valid when:

  1. Every opening bracket is closed by the same type of bracket.
  2. Open brackets are closed in the correct order.
  3. Every closing bracket has a corresponding opening bracket of the same type.

Return true when s is valid; otherwise, return false.

Function

isValidParentheses(s: String) → boolean

Examples

Example 1

s = "()"return = true

The opening parenthesis is closed by the matching closing parenthesis.

Example 2

s = "()[]{}"return = true

Each opening bracket closes with the same type, and no bracket closes out of order.

Example 3

s = "([)]"return = false

The closing parenthesis appears before the nested square bracket is closed.

Example 4

s = "([])"return = true

The square brackets close first, followed by the surrounding parentheses.

Constraints

  • 1 <= s.length <= 10^4.
  • Every character in s is one of ()[]{}.

More Meta problems

See Meta hiring insights
public boolean isValidParentheses(String s) {
    // write your code here
}
s"()"
expectedtrue
Checking account…