import java.util.*;
class Solution {
public int solution(String s) {
int answer = 0;
for(int i=0;i<s.length();i++){
StringBuilder sb=new StringBuilder();
sb.append(s.substring(i,s.length()));
sb.append(s.substring(0,i));
boolean ret=func(sb.toString());
if(ret)answer++;
}
return answer;
}
static boolean func(String str){
if(str.length()==1)return false;
Stack<Character> stack=new Stack<>();
int s=0,m=0,b=0;
for(int i=0;i<str.length();i++)stack.add(str.charAt(i));
char pre='.';
while(!stack.isEmpty()){
char c=stack.pop();
if(c==')')s++;
else if(c=='('){
if(s==0)return false;
if(pre=='}' || pre==']')return false;
else s--;
}
else if(c=='}')m++;
else if(c=='{'){
if(m==0)return false;
if(pre==')' || pre==']')return false;
else m--;
}
else if(c==']')b++;
else if(c=='['){
if(b==0)return false;
if(pre==')' || pre=='}')return false;
else b--;
}
pre=c;
}
if(s!=0 || m!=0 || b!=0)return false;
return true;
}
}
#스택