var isValid = function (s) {
let arr = s.split('')
if (arr.length % 2 !== 0) {
return false
}
let rightToLeft = {
')': '(',
']': '[',
'}': '{'
}
let stack = []
while (arr.length > 0) {
let s = arr.shift()
if (['(', '[', '{'].includes(s)) {
stack.push(s)
}
if ([')', ']', '}'].includes(s)) {
let left = stack.pop()
if(left === rightToLeft[s]) {
continue
} else {
return false
}
}
}
return stack.length === 0
};
var isValid = function (s) {
let arr = s.split('')
if (arr.length % 2 !== 0) {
return false
}
let rightToLeft = {
')': '(',
']': '[',
'}': '{'
}
let stack = []
while (arr.length > 0) {
let s = arr.shift()
if(stack.length === 0) {
stack.push(s)
} else {
let left = stack.pop()
if(left !== rightToLeft[s]) {
stack.push(left)
stack.push(s)
}
}
}
return stack.length === 0
};
console.log(isValid("()[]{}"))
console.log(isValid("{[]}"))
console.log(isValid("([)]"));