Problem Solving/Programmers

[Level 2] 괄호 회전하기

kmkunk 2022. 4. 6. 11:24
import java.util.*;

class Solution {
    public int solution(String s) {
        int answer = 0;
        int len = s.length();
        
        for(int i=0; i<len; i++) {
            Stack<Character> stack = new Stack<>();
            for(int j=0; j<len; j++) {
                char c = s.charAt(j);
                
                if(c=='[' || c=='(' || c=='{') { stack.push(c); }
                else {
                    if(stack.isEmpty()) {
                        stack.push(c);
                        break;
                    } else {
                        if((stack.peek()=='['&&c==']') || (stack.peek()=='('&&c==')') || (stack.peek()=='{'&&c=='}')) {
                            stack.pop();
                        }
                    }
                }
            }
            
            if(stack.isEmpty()) { answer++; }
            
            s = s.substring(1, len)+String.valueOf(s.charAt(0));
        }
        
        return answer;
    }
}