package com.company;
import java.util.Stack;
class Solution {
public int solution(String s) {
int answer = 0;
int sLength = s.length();
String editStr = s + s.substring(0, sLength - 1);
for (int i = 0; i < sLength; i++) {
String editTempStr = editStr.substring(i, i + sLength);
if(isRightBracket(editTempStr))
answer++;
}
return answer;
}
private boolean isRightBracket(String editTempStr) {
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < editTempStr.length(); i++) {
char tempChar = editTempStr.charAt(i);
if(!stack.isEmpty() && isClosedBracket(tempChar)){
char tempStackPeek = stack.peek();
if(tempChar == '}' && tempStackPeek == '{'){
stack.pop();
}else if(tempChar == ']' && tempStackPeek == '['){
stack.pop();
}else if(tempChar == ')' && tempStackPeek == '('){
stack.pop();
}else {
return false;
}
}else{
stack.push(tempChar);
}
}
return stack.isEmpty();
}
private boolean isClosedBracket(char tempChar) {
return tempChar == '}' || tempChar == ']' || tempChar == ')';
}
}