function isBalanced(str) {
const stack = [];
const openCloseMap = { "(": ")", "[": "]", "{": "}" };
const closingParens = new Set(Object.values(openCloseMap));
for (const c of str) {
if (openCloseMap[c]) {
stack.push(c);
} else if (closingParens.has(c) && (stack.length === 0 || openCloseMap[stack.pop()] !== c)) {
return false;
}
}
return stack.length === 0;
}