-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagainClassicalProblem.js
More file actions
53 lines (45 loc) · 1.22 KB
/
Copy pathagainClassicalProblem.js
File metadata and controls
53 lines (45 loc) · 1.22 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Q https://pdfhost.io/v/aB0p9oOtT_Again_a_classical_problem.pdf
let stack = [];
function main(array) {
let flag = true;
if (array.length % 2 === 1) {
return "not balanced";
} else if (array[0] === ")" || array[0] === "}" || array[0] === "]") {
return "not balanced";
}
for (let i = 0; i < array.length; i++) {
if (array[i] === "(" || array[i] === "{" || array[i] === "[") {
stack.push(array[i]);
} else if (array[i] === ")" || array[i] === "}" || array[i] === "]") {
let temp = stack.pop();
if (
(array[i] === ")" && temp !== "(") ||
(array[i] === "}" && temp !== "{") ||
(array[i] === "]" && temp !== "[")
) {
flag = false;
}
}
}
if (flag) {
return "balanced"
} else {
return "not balanced"
}
}
function runProgram(input) {
let newInput = input.trim().split("\n");
for (let i = 1; i < newInput.length; i++) {
let array = newInput[i].trim().split("");
console.log(main(array));
}
}
let input = `5
){(){[])}]
]]])}[(}}{
{{}}
[(){}{())[
[}]{(}[[}[`;
runProgram(input);
// balanced
// not balanced