[Algorithm - Programmers] 모음사전

nunu·2023년 12월 17일
0

Algorithm

목록 보기
120/142

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

제출 코드

import java.util.ArrayList;
import java.util.Collections;
class Solution {
    String[] chars = {"A", "E", "I", "O", "U"};
    ArrayList<String> words = new ArrayList<>();
    public int solution(String word) {
        int answer = 0;
        dfs(0, "");

        Collections.sort(words);
        for (answer = 0; answer < words.size(); answer++) {
            if (words.get(answer).equals(word)) {
                answer++;
                break;
            }
        }
        return answer;
    }
    void dfs(int depth, String word) {
        if (depth > 5)
            return;
        if (word.length() != 0 && !words.contains(word))
            words.add(word);
        for (int i = 0; i < chars.length; i++) {
            dfs(depth + 1, word + chars[i]);
        }
    }
}
profile
Hello, I'm nunu

0개의 댓글