Public
Edited
May 26, 2023
Insert cell
Insert cell
{
const errors = [];
for (const { str, expected } of testCases) {
if (isBalanced(str) !== expected) {
errors.push({ str, expected });
}
}
return errors;
}
Insert cell
Insert cell
isBalanced("())")
Insert cell
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;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more