FastPrepValid Parentheses

Valid Parentheses

Qualys logoQualys● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a string s containing only ()[]{}, determine whether its brackets are balanced. Every closing bracket must match the most recent unmatched opening bracket of the same type, and every opening bracket must be closed.

For this exercise, assume the empty string is valid. Return a boolean.

Function

validParentheses(s: String) → boolean

Examples

Example 1

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

Each pair has the correct type, and the square brackets close before their enclosing parentheses.

Example 2

s = "([)]"return = false

The first closing parenthesis encounters an unmatched opening square bracket, so the nesting is invalid.

Constraints

  • For this exercise, assume 0 <= s.length <= 10^5.
  • Every character is one of ( ) [ ] { }.

More Qualys problems

See Qualys hiring insights
public boolean validParentheses(String s) {
    // Write your solution here.
}
s"([]){}"
expectedtrue
Checking account…