슈콩

[프로그래머스] Lv.2 괄호 회전하기 본문

Algorithms/Programmers

[프로그래머스] Lv.2 괄호 회전하기

shukong 2025. 10. 14. 20:07

 

 

 

[문제]

https://school.programmers.co.kr/learn/courses/30/lessons/76502#

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

[소스 코드]

import java.util.*;
class Solution {
    public int solution(String s) {
        int answer = 0;
        int n = s.length();
        Stack<Character> stack;
        for(int i=0;i<n;i++){
            String back = s.substring(0,i);
            String front = s.substring(i);
            String result = front + back;
            stack = new Stack<>();
            boolean check = true;
            for(char c : result.toCharArray()){
                if(c==']'){
                    if(stack.isEmpty() || stack.peek()!='['){
                        check = false;
                        break;   
                    }
                    stack.pop();
                }
                else if(c=='}'){
                    if(stack.isEmpty() || stack.peek()!='{') {
                        check = false;
                        break;
                    }
                    stack.pop();
                }
                else if(c==')'){
                    if(stack.isEmpty() || stack.peek()!='(') {
                        check = false;
                        break;
                    }
                    stack.pop();
                }
                else stack.push(c);
            }
            if(stack.isEmpty() && check) answer++; 
        }
        return answer;
    }
}