Overview
Given a string containing brackets (), [], {} and other characters, determine whether every opening bracket has a corresponding closing bracket in the correct order.
Implement isBalanced(str) that returns true if all brackets are properly nested and matched, and false otherwise.
Constraints
- Input is always a string (may be empty).
- Only
(),[], and{}are considered brackets — all other characters should be ignored. - An empty string is considered balanced.
- Brackets must be closed in the correct order:
"([])"is valid,"([)]"is not. - Strings with only non-bracket characters are considered balanced.
Examples
isBalanced("()[]{}"); // true
isBalanced("([{}])"); // true
isBalanced("(]"); // false
isBalanced("([)]"); // false
isBalanced("a]b(c)[d]"); // false — stray closing bracket
isBalanced(""); // true
isBalanced("hello world"); // true — no bracketsNotes
- A classic stack-based approach works well here — push opening brackets, pop on closing brackets, and verify the match.
- Edge cases: strings with only opening brackets, only closing brackets, deeply nested brackets, and mixed non-bracket characters.
- Time complexity should be $O(n)$ where $n$ is the string length.
Solution
Reveal solution
function isBalanced(str) {
const stack = [];
const pairs = { ")": "(", "]": "[", "}": "{" };
const openers = new Set(["(", "[", "{"]);
for (const ch of str) {
if (openers.has(ch)) {
stack.push(ch);
} else if (pairs[ch] !== undefined) {
if (stack.length === 0 || stack.pop() !== pairs[ch]) return false;
}
}
return stack.length === 0;
}Resources
balanced-brackets.js
Balanced Brackets
easycodingAlgorithmsData Structures
Overview
Given a string containing brackets (), [], {} and other characters, determine whether every opening bracket has a corresponding closing bracket in the correct order.
Implement isBalanced(str) that returns true if all brackets are properly nested and matched, and false otherwise.
Constraints
- Input is always a string (may be empty).
- Only
(),[], and{}are considered brackets — all other characters should be ignored. - An empty string is considered balanced.
- Brackets must be closed in the correct order:
"([])"is valid,"([)]"is not. - Strings with only non-bracket characters are considered balanced.
Examples
isBalanced("()[]{}"); // true
isBalanced("([{}])"); // true
isBalanced("(]"); // false
isBalanced("([)]"); // false
isBalanced("a]b(c)[d]"); // false — stray closing bracket
isBalanced(""); // true
isBalanced("hello world"); // true — no bracketsNotes
- A classic stack-based approach works well here — push opening brackets, pop on closing brackets, and verify the match.
- Edge cases: strings with only opening brackets, only closing brackets, deeply nested brackets, and mixed non-bracket characters.
- Time complexity should be $O(n)$ where $n$ is the string length.
Solution
Reveal solution
function isBalanced(str) {
const stack = [];
const pairs = { ")": "(", "]": "[", "}": "{" };
const openers = new Set(["(", "[", "{"]);
for (const ch of str) {
if (openers.has(ch)) {
stack.push(ch);
} else if (pairs[ch] !== undefined) {
if (stack.length === 0 || stack.pop() !== pairs[ch]) return false;
}
}
return stack.length === 0;
}Resources
NameTopicDifficulty
103 of 103 problems