FastPrepValid Parentheses

Valid Parentheses

Amazon logoAmazon● EasyFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW
Learn

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 ()[]{}.

Source note: The prompt excerpt names parenthesis matching with a stack. The practice contract supplies the conventional bracket alphabet and edge cases omitted from the report.

More Amazon problems

See Amazon hiring insights
public boolean isValidParentheses(String s) {
    // write your code here
}
s"()[]{}"
expectedtrue
Checking account…