Skip to content

Instantly share code, notes, and snippets.

@MikeCheng1208
Created October 10, 2019 14:09
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/36d3e2a1e66a7c3588d6031e0331d4e5 to your computer and use it in GitHub Desktop.
Save MikeCheng1208/36d3e2a1e66a7c3588d6031e0331d4e5 to your computer and use it in GitHub Desktop.
/**
* @param {string} str
* @return {boolean}
*/
const isValid = function(str) {
const arr = [];
const map = {
"}": 1,
")": 2,
"]": 3,
}
for(let item of str){
if(item === '{'){
arr.push(1);
}else if(item === '('){
arr.push(2);
}else if(item === '['){
arr.push(3);
}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