Last active
June 20, 2018 01:15
-
-
Save basekays/8b59a40225f8ee5185f887b42b2953b4 to your computer and use it in GitHub Desktop.
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
| /** | |
| * @param {string} s | |
| * @return {boolean} | |
| */ | |
| const reference = { | |
| '(': ')', | |
| '{': '}', | |
| '[': ']', | |
| }; | |
| function isValid(string) { | |
| const stack = []; | |
| for (let i = 0; i < string.length; i++) { | |
| let target = string[i]; | |
| if (reference[target]) { | |
| stack.push(reference[target]); | |
| } else { | |
| if (target != stack.pop()) { | |
| return false; | |
| } | |
| } | |
| } | |
| return stack.length == 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment