We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7dadd5d commit b2fdfc6Copy full SHA for b2fdfc6
oh-chaeyeon/2주차_스택/Rotate_Parentheses.js
@@ -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