-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay75.js
More file actions
37 lines (34 loc) · 1.34 KB
/
Day75.js
File metadata and controls
37 lines (34 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//* Question: Balanced Brackets
// Write a function isBalanced(str) that takes a string str containing only parentheses ()
// and curly braces {}.
// The function should return true if every opening parenthesis has a corresponding closing parenthesis in the correct order,
// and every opening curly brace has a corresponding closing curly brace in the correct order. Otherwise, it should return false.
function isBalanced(str) {
const stack = [];
const pairs = {
'(': ')',
'{': '}',
};
for (let char of str) {
if (char === '(' || char === '{') {
stack.push(char);
} else if (char === ')' || char === '}') {
if (stack.length === 0) {
return false; // More closing brackets than opening brackets
}
let last = stack.pop();
if (pairs[last] !== char) {
return false; // Mismatched brackets
}
}
}
return stack.length === 0; // Ensure all opened brackets are closed
}
// Test cases
console.log(isBalanced("(){}")); // true
console.log(isBalanced("({})")); // true
console.log(isBalanced("({}[])")); // true
console.log(isBalanced("({)}")); // false
console.log(isBalanced("{[}]")); // false
console.log(isBalanced("(){}{}()")); // true
console.log(isBalanced("(){}{}()(")); // false