[프로그래머스][모음사전]-Lv.2

호준·2022년 11월 10일
0

Algorithm

목록 보기
98/111
post-thumbnail

🎊 문제

문제링크

🎊 제한사항

🎊 접근방법

  1. DFS를 통하여 순서에 따라 중복이 허용된 모든 경우의 수를 list에 담는다.
  2. 해당하는 word에 해당하는 list의 index를 구하고 반환한다.

🎊 코드

import java.util.*;
class Solution {
    static char[] ch = {'A', 'E', 'I', 'O', 'U'};
    static List<String> list = new ArrayList<>();
    public int solution(String word) {
        dfs(0,"",0);
        return list.indexOf(word);
    }
    void dfs(int depth, String cur, int count){
        if(depth<=5){
            list.add(cur);
            if(depth == 5){
                return;
            }
        }
        for(int i=0; i<5; i++){
            dfs(depth+1, cur+ch[i], count + 1);
        }
    }
}
profile
도전하지 않는 사람은 실패도 성공도 없다

0개의 댓글