Problem · String

Valid Parentheses

Learn this problem
EasyZoom logoZoomFULLTIMEONSITE INTERVIEW

Problem statement

Given a string s containing only parentheses, square brackets, and curly braces, return whether it is valid.

A string is valid when all of these rules hold:

  • Every opening bracket is closed by the same bracket type.
  • Opening brackets are closed in reverse order of how they were opened.
  • Every closing bracket has a matching earlier opening bracket.

The empty string is valid.

Function

isValidParentheses(s: String) → boolean

Examples

Example 1

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

Each adjacent pair has the correct matching type.

Example 2

s = "([)]"return = false

The square bracket opens after the parenthesis, so it must close before the parenthesis.

Example 3

s = "{[]}"return = true

The brackets are correctly nested and every opener is closed.

Constraints

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

More Zoom problems

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