966. Vowel Spellchecker

ss
3 min readMar 23, 2021

--

檢查拼字錯誤, 會先給一組正確的拼字, 再給一組輸入, 檢查拼字錯誤並且回傳正確的, 若都沒有正確的字串, 則回傳空

判定依據先後順序是

  1. 全部一樣

2. 大小寫錯而已

3. 母音錯而已, 例如 ‘e’ 變 ‘a’

class Solution {
public:
vector<string> spellchecker(vector<string> &wordlist,
vector<string> &queries) {
vector<string> res;
unordered_set<string> st(wordlist.begin(), wordlist.end());
unordered_map<string, string> lower_st;
unordered_map<string, string> vowel_error_st;
for(auto s: wordlist){
string lowerString;
string vowelString;
for(auto c: s){
c= tolower(c);
lowerString += c;
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){
vowelString += '*';
}else{
vowelString += c;
}
}
if(!lower_st.count(lowerString)) lower_st[lowerString] = s;

if(!vowel_error_st.count(vowelString)) vowel_error_st[vowelString] = s;
}
for (auto q: queries) {
if(st.count(q)){
res.push_back(q);
continue;
}
string lowerString;
string vowelString;
for(auto c: q){
c = tolower(c);
lowerString += c;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
vowelString += '*';
} else {
vowelString += c;
}
}

if(lower_st.count(lowerString)){
res.push_back(lower_st[lowerString]);
continue;
}
if(vowel_error_st.count(vowelString)){
res.push_back(vowel_error_st[vowelString]);
continue;
}
res.push_back("");
}
return res;
}
};

就是給三個順位的hashmap, 然後對應即可

這種題目都有點冗…

--

--

ss
ss

No responses yet