Skip to content

Commit b2fdfc6

Browse files
committed
Rotate Parentheses / 심화
1 parent 7dadd5d commit b2fdfc6

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function solution(s) {
2+
let answer = 0;
3+
for (let i = 0; i < s.length; i++) {
4+
const rotated = s.slice(i) + s.slice(0, i);
5+
if (isValid(rotated)) answer++;
6+
}
7+
return answer;
8+
}
9+
10+
function isValid(str) {
11+
const stack = [];
12+
for (let c of str) {
13+
if ("([{".includes(c)) stack.push(c);
14+
else if (!stack.length || !isPair(stack.pop(), c)) return false;
15+
}
16+
return stack.length === 0;
17+
}
18+
19+
function isPair(open, close) {
20+
return (
21+
(open === "(" && close === ")") ||
22+
(open === "[" && close === "]") ||
23+
(open === "{" && close === "}")
24+
);
25+
}

0 commit comments

Comments
 (0)