FastPrepValid Parentheses
Problem · String

Valid Parentheses

Learn this problem
EasySalesforce logoSalesforceFULLTIMEONSITE INTERVIEW
See Salesforce hiring insights

Problem statement

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

A bracket string is valid when both conditions hold:

  • Every opening bracket is closed by the same type of bracket.
  • Brackets close in the reverse order in which they were opened.

Function

isValidParentheses(s: String) → boolean

Examples

Example 1

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

Each opening bracket is immediately closed by the matching type, so the string is valid.

Example 2

s = "(]"return = false

The opening parenthesis cannot be closed by ].

Example 3

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

The bracket types are properly nested and close from the innermost pair outward.

Constraints

  • 1 <= s.length <= 10^5
  • s contains only ()[]{}.

More Salesforce problems

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