본문 바로가기
알고리즘/프로그래머스

5주차 모음사전 (자바)

by 김어찐 2021. 9. 14.
728x90
package prog;

public class 오주차_모음사전 {
    public static void main(String[] args) {
        Solution_모음사전 s= new Solution_모음사전();
//        System.out.println(s.solution("AAAAE"));
//        System.out.println(s.solution("AAAE"));
        System.out.println(s.solution("I"));
//        System.out.println(s.solution("EIO"));


    }
}

class Solution_모음사전{
    static char[] M = new char[]{'A', 'E', 'I', 'O', 'U'};
    static int count=0;
    static boolean check=false;
    static String collect;
    static int answer = 0;
    public int solution(String word) {
        answer = 0;
        collect = word;
        StringBuilder sb = new StringBuilder();
        dfs(sb);
        return answer;
    }

    private void dfs(StringBuilder sb) {

        String tmpStr = sb.toString();
        if (tmpStr.equals(collect)) {
            check=true;
            answer = count;
            return;
        }
        if(check || sb.length()>=5) return;
        for (int i = 0; i < M.length; i++) {
            count ++;
            dfs(sb.append(M[i]));
            sb.deleteCharAt(sb.toString().length()-1);
        }
    }
}
728x90

'알고리즘 > 프로그래머스' 카테고리의 다른 글

프로그래머스 튜플 (자바)  (0) 2021.09.29
행렬 테두리 회전 (자바)  (0) 2021.09.23
뉴스 클러스터링 (자바)  (0) 2021.09.12
표편집 (자바)  (0) 2021.09.12
짝지어 제거하기 (자바)  (0) 2021.09.09