Skip to content

Instantly share code, notes, and snippets.

@MikeCheng1208
Last active October 10, 2019 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeCheng1208/bfaf28b96db0fd7b72618d1e4400c4d7 to your computer and use it in GitHub Desktop.
Save MikeCheng1208/bfaf28b96db0fd7b72618d1e4400c4d7 to your computer and use it in GitHub Desktop.
/**
* @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