DIY: Valid Parentheses

Solve the interview question "Valid Parentheses" in this lesson.

Problem statement

For this problem, you are given a string that may be empty or may consist of opening and closing parentheses. Your task is to check whether the string contains valid parenthesization or not.

Constraints

  • 1 <= string.length <= 10410^4
  • The string will only contain parentheses (), square brackets [], and braces {}.

Input

The function will take a string. Here is an example of the input:

input = "(){[{()}]}"

Output

The function will return true if the given string contains a properly matched sequence of brackets. Otherwise, it will return false. The following is the output of the input given above:

true

Coding exercise

For this coding exercise, you need to implement the valid(string) function, wherein the string will either be empty or a set of parentheses.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.