
10. Rotate Parenthesis
Sunhyup Lee. (2024). To be a successful candidate for the coding test. goldenrabbit. p.155.
Problem solving
function solution(s) {
const n = s.length;
let answer = 0;
// ① s.length-1 회전하면 모든 경우의 수 파악 가능
for (let i = 0; i < s.length; i++) {
// ② 괄호 문자열을 회전시키면서 참조
const stack = [];
let isCorrect = true;
for (let j = 0; j < n; j++) {
const c = s[(i + j) % n];
if (c === "[" || c === "(" || c === "{") {
// ③ 열린 괄호는 푸시
stack.push(c);
} else {
if (stack.length === 0) {
// ④ 여는 괄호가 없는 경우
isCorrect = false;
break;
}
// ⑤ 닫힌 괄호는 스택의 top과 짝이 맞는지 비교
const top = stack[stack.length - 1];
if (c === "]" && top === "(") {
stack.pop();
} else if (c === "}" && top === "{") {
stack.pop();
} else {
isCorrect = false;
break;
}
}
}
// ⑥ 모든 괄호의 짝이 맞는 경우
if (isCorrect && stack.length === 0) {
answer += 1;
}
}
return answer;
}

rotate parenthesis solution
Jay Tak