Skip to content

Commit e2458e1

Browse files
committed
Rotating brackets / 심화
1 parent a96708a commit e2458e1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function solution(s) {
2+
let result = 0;
3+
const map = { ")": "(", "}": "{", "]": "[" };
4+
const arr = [...s];
5+
6+
const isValid = (arr) => {
7+
const stack = [];
8+
9+
for (let i = 0; i < arr.length; i++) {
10+
const item = arr[i];
11+
const mappingItem = map[item];
12+
if (mappingItem) {
13+
if (mappingItem === stack.at(-1)) stack.pop();
14+
else return false;
15+
} else {
16+
stack.push(item);
17+
}
18+
}
19+
return stack.length === 0;
20+
};
21+
22+
for (let i = 0; i < s.length; i++) {
23+
if (isValid(arr)) result++;
24+
arr.unshift(arr.pop());
25+
}
26+
27+
return result;
28+
}

0 commit comments

Comments
 (0)