Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* @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