FastPrepValid Parentheses

Valid Parentheses

BlackRock logoBlackRock● EasyNEW GRADOA
Learn

Problem statement

Given a string s containing only (, ), [, ], {, 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 correct nested order. The empty string is valid.

Function

isValidParentheses(s: String) → boolean

Examples

Example 1

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

Every bracket closes in the reverse order in which it opened.

Example 2

s = "([)]"return = false

The closing parenthesis appears before the square bracket is closed.

Constraints

  • 0 <= s.length <= 100000.
  • s contains only the six bracket characters listed above.

More BlackRock problems

See BlackRock hiring insights
public boolean isValidParentheses(String s) {
    // Write your code here.
}
s"([]{})"
expectedtrue
Checking account…