-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCC-23.js
36 lines (31 loc) · 1.27 KB
/
DCC-23.js
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
// Prompt:
// - Write a function called balancedBrackets that accepts a single string as argument.
// - The input string is composed entirely of parentheses, brackets and/or curly braces, i.e., (), [] and/or {}. Referred to as "braces" from this point forward...
// - The balancedBraces function should return true if the string's braces are "balanced" and false if they are not.
// - The brackets are considered unbalanced if any closing bracket does not close the same type of opening bracket, ignoring already matched brackets between them. Examples explain it best...
// Examples:
// balancedBrackets( '()' ) // => true
// balancedBrackets( '(]' ) // => false
// balancedBrackets( '[{}]' ) // => true
// balancedBrackets( '[(])' ) // => false
// balancedBrackets( '[({}[])]' ) // => true
const balancedBrackets = (str) => {
const stack = [];
const openingBraces = ["(", "[", "{"];
const closingBraces = [")", "]", "}"];
for (let i = 0; i < str.length; i++) {
const brace = str[i];
if (openingBraces.includes(brace)) {
stack.push(brace);
} else if (closingBraces.includes(brace)) {
const lastOpeningBrace = stack.pop();
if (
openingBraces.indexOf(lastOpeningBrace) !==
closingBraces.indexOf(brace)
) {
return false;
}
}
}
return stack.length === 0;
};