Last active
October 10, 2019 14:06
-
-
Save MikeCheng1208/bfaf28b96db0fd7b72618d1e4400c4d7 to your computer and use it in GitHub Desktop.
This file contains 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} str | |
* @return {boolean} | |
*/ | |
const isValid = function(str) { | |
const arr = []; | |
const map = { | |
"}": "{", | |
")": "(", | |
"]": "[", | |
} | |
for(let item of str){ | |
if(item === '{' || item === '(' || item === '['){ | |
arr.push(item); | |
}else{ | |
if(arr.pop() !== map[item] ){ | |
return false; | |
} | |
} | |
} | |
return arr.length === 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment