Problem
- We’re given a a string
s
that contains the following characters:
'('
, ')'
, '{'
, '}'
, '['
and ']'
- Our function should return if the input is valid based on the following conditions:
- Every open bracket has its respective closing bracket
- Open brackets are closed in the correct order
- Every close bracket has its respective open bracket
Example
Approach 1
Visualize the problem
Plan
- Go through input string
- If the current character is a opening bracket then we can look for the next character to have its closing bracket
- If the current character is not the respective closing bracket return False
Implementation
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)