-
Notifications
You must be signed in to change notification settings - Fork 0
/
p괄호 회전하기.js
57 lines (54 loc) · 1.02 KB
/
p괄호 회전하기.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const solution = (s) => {
let answer = 0;
s = s.split("");
for (let i = 0; i < s.length; i++) {
s.push(s.shift());
if (findCase(s)) {
answer++;
}
}
return answer;
};
const findCase = (s) => {
let answer = 0;
let stack = [];
for (let i of s) {
switch (i) {
case "[":
case "{":
case "(":
stack.push(i);
break;
case "]":
if (stack.pop() === "[") {
answer += 1;
break;
} else {
return 0;
}
case "}":
if (stack.pop() === "{") {
answer += 1;
break;
} else {
return 0;
}
case ")":
if (stack.pop() === "(") {
answer += 1;
break;
} else {
return 0;
}
}
}
if (stack.length > 0) {
return 0;
}
return answer;
};
console.log(solution("[](){}")); //3
console.log(solution("}]()[{")); //2
console.log(solution("[)(]")); //0
console.log(solution("}}}")); //0
console.log(solution("{{")); //0