Created
August 9, 2021 01:53
-
-
Save Rutvik17/345faeeafb5f6b3e443b3c7bcfd1d841 to your computer and use it in GitHub Desktop.
Valid Parentheses LeetCode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. | |
| An input string 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: s = "()" | |
| Output: true | |
| Example 2: | |
| Input: s = "()[]{}" | |
| Output: true | |
| Example 3: | |
| Input: s = "(]" | |
| Output: false | |
| Example 4: | |
| Input: s = "([)]" | |
| Output: false | |
| Example 5: | |
| Input: s = "{[]}" | |
| Output: true | |
| Constraints: | |
| 1 <= s.length <= 104 | |
| s consists of parentheses only '()[]{}'. | |
| */ | |
| const isValidParantheses = (s) => { | |
| let map = { | |
| "(" : ")", | |
| "{" : "}", | |
| "[" : "]" | |
| } | |
| let stack = []; | |
| for (let i = 0; i < s.length; i++) { | |
| if (s[i] === '(' || s[i] === '{' || s[i] === '[') { | |
| stack.push(s[i]) | |
| } else { | |
| let last = stack.pop(); | |
| if (s[i] !== map[last]) { | |
| return false; | |
| } | |
| } | |
| } | |
| if (stack.length !== 0) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| const s = "()[]{}" | |
| console.log(isValidParantheses(s)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment