프로그래머스 - 모음사전

greenTea·2023년 5월 25일
0

코드

class Solution {
    
    List<String> list = new ArrayList<>();
    
    public int solution(String word) {
        dfs("");
        return list.indexOf(word);
    }
    
    void dfs(String str) {

        if (str.length() > 5) {
            return;
        }
        
        list.add(str);
        
        for (int i=0; i < 5; i++) {
            dfs(str + "AEIOU".charAt(i));
        }
    }
}

풀이

🫡중복 조합으로 풀 수 있는 문제이다. 모든 경우의 수를 구하여 list에 저장한 후 indexOf를 통해 해당 값을 가져오면 정답이다.

🤔처음에는 따로 String 배열을 만들어 AEIOU를 저장한 후 list에 모든 갑이 들어오고 나면 sort를 해주었는데 다른 분들의 풀이를 보니 charAt를 활용하여 푸는 것을 보았으며 sort가 따로 없는 것을 보고 sort할 필요가 없다는 것을 알았다.

출처 : 프로그래머스 - 모음사전

profile
greenTea입니다.

0개의 댓글