Data Structures and Algorithms Questions

3 / 13

Validate Parentheses

Given a string P containing just the characters (, ), {, }, [ and ], determine if the string P is valid.

An input string P is valid if:

  • Open brackets must be closed by the same type of brackets.
  • Open brackets must be closed in the correct order.

Example 1:

 Input: P = "()"
 Output: True

Example 2:

 Input: P = "()[]{}"
 Output: True

Example 3:

 Input: P = "(]"
 Output: False

Example 4:

 Input: P = "([)]"
 Output: False

Example 5:

 Input: P = "{[]}"
 Output: True

Constraints:

  • 1 <= len(P)<= 104
  • P consists of parentheses only ()[]{}.
INSTRUCTIONS

  1. Write your code inside a function named validate_parentheses
  2. There are no partial marks for the question.
  3. Your function must return the output, it should not print the output.
  4. To execute a block on the right-side coding panel, please press 'shift'+ 'enter' inside the block.
  5. Your code should work for all permitted possible values(check Constraints) of P

Complete the below code in the right side coding panel

def validate_parentheses(P: str) -> bool:
    # your code goes here
See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...