알고리즘/리트코드

리트코드 데일리과제(3/22) - Vowel Spellchecker

leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/591/week-4-march-22nd-march-28th/3681/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

 

1. 유형

해시맵

 

2. 풀이

 

문제 해석

 

쿼리가 단어 목록의 단어(대소문자 구분)와 정확히 일치하면 동일한 단어를 반환해야 합니다.


쿼리가 단어를 대문자로 일치시킬 경우, 단어 목록에서 첫 번째 일치 항목을 반환해야 합니다.

 

쿼리가 단어를 모음 오류까지 일치시킬 경우, 단어 목록에서 첫 번째 일치 항목을 반환해야 합니다.

 

쿼리에 단어 목록에 일치하는 항목이 없으면 빈 문자열을 반환해야 합니다.

 

3. 코드

class Solution {
    static String change(String str){
        String res="";
        for(int i=0; i<str.length(); i++){
            if(str.charAt(i) == 'A' || str.charAt(i) == 'E' || str.charAt(i) == 'I' || str.charAt(i) == 'U'
              ||str.charAt(i) == 'O'){
                res+='*';
            }else
                res+=str.charAt(i);
        }
        return res;
    }
    public String[] spellchecker(String[] wordlist, String[] queries) {
        String output[] = new String[queries.length];
        HashMap<String, String> diction = new HashMap<>();
        for(int i=0; i<wordlist.length; i++){
            diction.put(wordlist[i], wordlist[i]);
            if(!diction.containsKey(wordlist[i].toUpperCase())){
                diction.put(wordlist[i].toUpperCase(), wordlist[i]);
            }
            if(!diction.containsKey(change(wordlist[i].toUpperCase() ))){
                diction.put(change(wordlist[i].toUpperCase()), wordlist[i]);
            }
        }
        for(int i=0; i<queries.length; i++){
            if(diction.containsKey(queries[i])){
                output[i] = diction.get(queries[i]);
                continue;
            }
            if(diction.containsKey(queries[i].toUpperCase())){
                output[i] = diction.get(queries[i].toUpperCase());
                continue;
            }
            if( diction.containsKey(change(queries[i].toUpperCase() )) ){
                output[i] = diction.get(change(queries[i].toUpperCase()));
                continue;
            }
            output[i] = "";
        }
        return output;
    }
}

 

모범답안

class Solution {
    Set<String> words_perfect;
    Map<String, String> words_cap;
    Map<String, String> words_vow;

    public String[] spellchecker(String[] wordlist, String[] queries) {
        words_perfect = new HashSet();
        words_cap = new HashMap();
        words_vow = new HashMap();

        for (String word: wordlist) {
            words_perfect.add(word);

            String wordlow = word.toLowerCase();
            words_cap.putIfAbsent(wordlow, word);

            String wordlowDV = devowel(wordlow);
            words_vow.putIfAbsent(wordlowDV, word);
        }

        String[] ans = new String[queries.length];
        int t = 0;
        for (String query: queries)
            ans[t++] = solve(query);
        return ans;
    }

    public String solve(String query) {
        if (words_perfect.contains(query))
            return query;

        String queryL = query.toLowerCase();
        if (words_cap.containsKey(queryL))
            return words_cap.get(queryL);

        String queryLV = devowel(queryL);
        if (words_vow.containsKey(queryLV))
            return words_vow.get(queryLV);

        return "";
    }

    public String devowel(String word) {
        StringBuilder ans = new StringBuilder();
        for (char c: word.toCharArray())
            ans.append(isVowel(c) ? '*' : c);
        return ans.toString();
    }

    public boolean isVowel(char c) {
        return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
    }
}

 

4. 새로 안 지식

 

containsKey()를 사용해서 없으면 추가 하는 식으로 코드를 짰지만

 

이미 putIfAbsent()라는 라이브러리가 있었다.

 

문자열을 추가할때는, toCharArray()사용해서 for each로 더 깔끔하게 넣을 수 있음.

 

3항 연산자 사용.

 

StringBuilder도 잘 사용 안했는데, 이번 계기로 사용을 해봐야할듯 싶다.