문제
괄호 회전하기
문제풀이
function checkBracket (arr) {
var stack = arr.slice();
var bracketStack = [];
var bool = false;
if (stack.length % 2 == 1) return false;
else {
while (stack.length != 0) {
var popElement = stack.pop();
if (popElement == ')' || popElement == '}' || popElement == ']' ) {
bracketStack.push(popElement);
}
else if (popElement == '(' || popElement == '{' || popElement == '[' ) {
var popBracket = bracketStack.pop();
var comb = popElement + popBracket;
if (! (comb == '()' || comb == '{}' || comb == '[]') ) {
return false;
}
}
}
}
if (bracketStack.length == 0) return true;
else return false;
}
function solution(s) {
var answer = 0;
var sArr = [...s];
for (var i = 0; i < sArr.length; i++) {
var bool = checkBracket(sArr);
if (bool) answer++;
sArr.push(sArr.shift());
}
return answer;
}