FastPrepValid Bracket Sequence

Valid Bracket Sequence

NVIDIA logoNVIDIA● EasyFULLTIMEOA
Learn

Problem statement

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

Every opening bracket must be closed by the same bracket type, and closing brackets must appear in the reverse order of their matching openings. A closing bracket without a prior unmatched opening is invalid. The empty string is valid.

Function

isValidBracketSequence(s: String) → boolean

Examples

Example 1

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

Each closer matches the newest unmatched opener of the same type.

Example 2

s = "([)]"return = false

The closing parenthesis arrives while the square bracket is still open.

Example 3

s = "((("return = false

Three opening brackets remain unmatched.

Example 4

s = ""return = true

There are no unmatched brackets in the empty sequence.

Constraints

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

More NVIDIA problems

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