forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parenthesis_Checker.js
41 lines (38 loc) · 1.24 KB
/
Parenthesis_Checker.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
38
39
40
41
function checkclosingParen(lastParen, currParen) {
return (
(lastParen === "(" && currParen === ")") ||
(lastParen === "[" && currParen === "]") ||
(lastParen === "{" && currParen === "}")
);
}
function BalancedParen(parenString) {
let pseudoStack = []; // Using Array(object) as stack therefore named as pseudoStack
for (let i = 0; i < parenString.length; i++) {
if (
parenString[i] == "(" ||
parenString[i] == "[" ||
parenString[i] == "{"
) {
pseudoStack.push(parenString[i]);
} else {
if (pseudoStack.length === 0) return "Unbalanced";
else if (
checkclosingParen(pseudoStack[pseudoStack.length - 1], parenString[i])
)
pseudoStack.pop();
else return "Unbalanced";
}
}
if (pseudoStack.length === 0) return "Balanced";
else return "Unbalanced";
}
let parenString1 = "[]{}()";
let parenString2 = "[{}()]";
let parenString3 = "[{()}]";
let parenString4 = "][(){";
let parenString5 = "[{]}";
console.log(BalancedParen(parenString1)); // Balanced
console.log(BalancedParen(parenString2)); // Balanced
console.log(BalancedParen(parenString3)); // Balanced
console.log(BalancedParen(parenString4)); // Unbalanced
console.log(BalancedParen(parenString5)); // Unbalanced