Skip to content
intermediate

Balanced delimiters

Complete the stack that matches brackets — the push, the pop, and the emptiness check that together decide balance.

/**
 * Returns true when every bracket in the input is matched and nested
 * correctly. Non-bracket characters do not affect the result.
 */
export function isBalanced(input: string): boolean {
  const ____: string[] = []
  const pairs: Record<string, string> = { ')': '(', ']': '[', '}': '{' }

  for (const ch of input) {
    if (ch === '(' || ch === '[' || ch === '{') {
      open.____(ch)
    } else if (pairs[ch] !== undefined) {
      // A closer must match the most recent unmatched opener.
      if (open.___() !== pairs[ch]) {
        return false
      }
    }
  }

  return open.______ === 0
}
0/4 filled tab moves · Ctrl+↵ submits · Ctrl+Shift+↵ runsstarter code