Start learning free

LESSON 317 OF 410 · STACK

Valid Parentheses

Practice O(n)

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

An input string is valid if:

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

Example 1:

Input: input = "()"
Output: True

Example 2:

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

Example 3:

Input: input = "(]"
Output: False

Example 4:

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

Example 5:

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

Constraints:

  • The length of input is between 1 and 104.
  • input consists of parentheses only '()[]{}'.

Note:

Your algorithm should run in O(n) time and use O(n) extra space.


STEP 1 OF 11
    Ctrl + Enter to check