forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-BalancedSymbols.js
37 lines (33 loc) · 919 Bytes
/
03-BalancedSymbols.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
37
function matches(open, close){
var opens = "([{",
closers = ")]}";
return opens.indexOf(open) == closers.indexOf(close);
}
function parenthesesChecker(symbols){
var stack = new Stack(),
balanced = true,
index = 0,
symbol, top;
while (index < symbols.length && balanced){
symbol = symbols.charAt(index);
if (symbol == '('|| symbol == '[' || symbol == '{'){
stack.push(symbol);
} else {
if (stack.isEmpty()){
balanced = false;
} else {
top = stack.pop();
if (!matches(top, symbol)){
balanced = false;
}
}
}
index++;
}
if (balanced && stack.isEmpty()){
return true;
}
return false;
}
console.log(parenthesesChecker('{{([][])}()}'));
console.log(parenthesesChecker('[{()]'));