1. 程式人生 > >[LeetCode] 966. Vowel Spellchecker

[LeetCode] 966. Vowel Spellchecker

題:https://leetcode.com/problems/vowel-spellchecker/

題目大意

參考原文

解題思路

1.生成 oWordSet 記錄所有wordlist ,若query 屬於那麼返回
2.生成cWordIndexMap 。 遍歷wordlist,將元素變為全小寫 。將小寫的元素作為key,原元素作為val,放入cWordIndexMap 中。若key已經出現,則不放入。
3.生成 vWordIndexMap。遍歷wordlist將元素變為全小寫,在將小寫元素中的母音字元變為 *,將新生成的元素作為key,原元素作為val,放入vWordIndexMap中。若key已經出現,則不放入。
4.每個query ,依次查詢上述資料集合。若都沒有返回""。

class Solution {
    public String[] spellchecker(String[] wordlist, String[] queries) {
        Set<Character> vowelsSet = new HashSet<>();

        vowelsSet.add('a');
        vowelsSet.add('e');
        vowelsSet.add('i');
        vowelsSet.add('o');
        vowelsSet.add('u');

        Set<
String>
oWordSet = new HashSet<>(); for(String word : wordlist) oWordSet.add(word); Map<String,String> cWordIndexMap = new HashMap<>(); for(String word : wordlist){ String lowerCaseWord = word.toLowerCase(); if(!cWordIndexMap.
containsKey(lowerCaseWord)) cWordIndexMap.put(lowerCaseWord,word); } Map<String,String> vWordIndexMap = new HashMap<>(); for(String word : wordlist){ String lowerCaseWord = word.toLowerCase(); StringBuilder strbuilder = new StringBuilder(); for(char c : lowerCaseWord.toCharArray()){ if(vowelsSet.contains(c)) strbuilder.append('*'); else strbuilder.append(c); } if(!cWordIndexMap.containsKey(strbuilder.toString())) cWordIndexMap.put(strbuilder.toString(),word); } String[] resArr =new String[queries.length]; int i = 0; for(String query :queries){ resArr[i++] = eleSpellchecker(vowelsSet,oWordSet,cWordIndexMap,vWordIndexMap,query); } return resArr; } private String eleSpellchecker(Set<Character> vowelsSet, Set<String> oWordSet, Map<String, String> cWordIndexMap, Map<String, String> vWordIndexMap, String query) { if(oWordSet.contains(query)){ return query; } String lowerCaseWord = query.toLowerCase(); if(cWordIndexMap.containsKey(lowerCaseWord)) return cWordIndexMap.get(lowerCaseWord); StringBuilder strbuilder = new StringBuilder(); for(char c : lowerCaseWord.toCharArray()){ if(vowelsSet.contains(c)) strbuilder.append('*'); else strbuilder.append(c); } if(cWordIndexMap.containsKey(strbuilder.toString())) return cWordIndexMap.get(strbuilder.toString()); return ""; } }

2.生成cWordIndexMap、vWordIndexMap 可以合併。使得程式碼更簡單

class Solution {
    public String[] spellchecker(String[] wordlist, String[] queries) {
        Set<Character> vowelsSet = new HashSet<>();

        vowelsSet.add('a');
        vowelsSet.add('e');
        vowelsSet.add('i');
        vowelsSet.add('o');
        vowelsSet.add('u');

        Set<String> oWordSet = new HashSet<>();
        for(String word : wordlist)
            oWordSet.add(word);
        Map<String,String> cWordIndexMap = new HashMap<>();
        Map<String,String> vWordIndexMap = new HashMap<>();

        for(String word : wordlist){
            String lowerCaseWord = word.toLowerCase();
            if(!cWordIndexMap.containsKey(lowerCaseWord))
                cWordIndexMap.put(lowerCaseWord,word);
            StringBuilder strbuilder = new StringBuilder();
            for(char c : lowerCaseWord.toCharArray()){
                if(vowelsSet.contains(c))
                    strbuilder.append('*');
                else
                    strbuilder.append(c);
            }
            if(!cWordIndexMap.containsKey(strbuilder.toString()))
                cWordIndexMap.put(strbuilder.toString(),word);
        }

        String[] resArr =new String[queries.length];
        int i = 0;
        for(String query :queries){
            resArr[i++] = eleSpellchecker(vowelsSet,oWordSet,cWordIndexMap,vWordIndexMap,query);
        }
        return resArr;
    }

    private String eleSpellchecker(Set<Character> vowelsSet, Set<String> oWordSet, Map<String, String> cWordIndexMap, Map<String, String> vWordIndexMap, String query) {
        if(oWordSet.contains(query)){
            return query;
        }
        String lowerCaseWord = query.toLowerCase();
        if(cWordIndexMap.containsKey(lowerCaseWord))
            return cWordIndexMap.get(lowerCaseWord);

        StringBuilder strbuilder = new StringBuilder();
        for(char c : lowerCaseWord.toCharArray()){
            if(vowelsSet.contains(c))
                strbuilder.append('*');
            else
                strbuilder.append(c);
        }
        if(cWordIndexMap.containsKey(strbuilder.toString()))
            return cWordIndexMap.get(strbuilder.toString());
        return "";
    }
}