import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class Solution {
    static HashSet<String> dictionary;
    public int solution(String word) {
        int answer = 0;
        String[] words = {"A","E","I","O","U"};
        dictionary = new HashSet<>();
        makeDictionary("",words);
     
        List<String> dict = new ArrayList<>(dictionary);
        Collections.sort(dict);
        
        for(int i=0; i<dict.size(); i++){
            if(dict.get(i).equals(word)){
                 answer=i;
                 break;
            }
        }
        return answer;
    }
    
    static void makeDictionary(String s,String[] words){
        if(s.length()>5) return;
        
        dictionary.add(s);
        for(int i=0; i<5; i++){
            makeDictionary(s+words[i],words);
        }
    }
}

 

중복이 없는 HashSet을 이용한 후 ArrayList를 통해 정렬

+ Recent posts