function isValid(str) {
const pairs = {
'(': ')',
'{': '}',
'[': ']'
};
const closes = Object.values(pairs);
const stack = [];
for (let s of str) {
if (pairs[s]) {
stack.push(pairs[s]);
} else if (closes.includes(s) && s !== stack.pop()) {
return false;
}
}
return !stack.length;
}