FastPrepValid Parentheses
Problem · String

Valid Parentheses

Learn this problem
EasyByteDance logoByteDanceFULLTIMEPHONE SCREEN

Problem statement

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

A string is valid when every opening bracket is closed by the same type of bracket and brackets close in the reverse order in which they were opened. The empty string is valid.

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

Function

isValidParentheses(s: String) → boolean

Examples

Example 1

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

Each opening bracket is immediately followed by its matching closing bracket.

Example 2

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

The brackets are properly nested and close in reverse opening order.

Example 3

s = "([)]"return = false

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

Constraints

  • 0 <= s.length <= 100000.
  • Every character of s is one of ()[]{}.

More ByteDance problems

drafts saved locally
public boolean isValidParentheses(String s) {
    // write your code here
}
s"()[]{}"
expectedtrue
checking account