查找元素用map
按照词条搜索,超时,根据solution,用map、hash map更好。
class Solution {
public:
// 只要在此表里,不一定是一一对应的。
// 规则有先后顺序,完全匹配》差大小写》差原音匹配(原始和替换部分都是原音)》错误
// 长度一定相同,不然一定是false
bool iscase1(string word, string query){
for(int i=0;i<word.size();i++){
if (word[i]!=query[i]){
return false;
}
}
return true;
}
bool iscase2(string word, string query){
for(int i=0;i<word.size();i++){
if (word[i]!=query[i]){ // remove 'a' 'A' cases
if (word[i]>='A'&&word[i]<='Z'){
word[i] = word[i]-'A'+'a';
}
if (query[i]>='A'&&query[i]<='Z'){
query[i] = query[i]-'A'+'a';
}
if (word[i]!=query[i]){
return false;
}
}
}
return true;
}
bool iscase3(string word, string query){
for(int i=0;i<word.size();i++){
if (word[i]!=query[i]){ // remove 'a' 'A' cases
if (word[i]>='A'&&word[i]<='Z'){
word[i] = word[i]-'A'+'a';
}
if (query[i]>='A'&&query[i]<='Z'){
query[i] = query[i]-'A'+'a';
}
if ((word[i]=='a' || word[i]=='e' || word[i]=='i'||word[i]=='o'||word[i]=='u')&&(query[i]=='a' || query[i]=='e' || query[i]=='i'||query[i]=='o'||query[i]=='u')){
continue;
}
else if (word[i]==query[i]){
continue;
}
else{
return false;
}
}
}
return true;
}
int func(string word, string query){
if (iscase1(word, query)){
return 1;
}
else if (iscase2(word, query)){
return 2;
}
else if (iscase3(word, query)){
return 3;
}
else{
return 4;
}
}
string func2(vector<string>& words, string query){
int min = 4;
string minwords="";
for (int i=0;i<words.size();i++){
int res = func(words[i], query);
//cout<<words[i]<<" "<<res<<endl;
if (res<min){
min = res;
minwords = words[i];
}
}
return minwords;
}
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {
map<int, vector<string>> len2words;
for (int i=0;i<wordlist.size();i++){
int len = wordlist[i].size();
if (len2words.find(len)!=len2words.end()){
len2words[len].push_back(wordlist[i]);
}
else{
vector<string> temp(1, wordlist[i]);
len2words[len] = temp;
}
}
vector<string> res;
for (int i=0;i<queries.size();i++){
string query = queries[i];
vector<string> words = len2words[query.size()];
string temp = func2(words, query);
res.push_back(temp);
}
return res;
}
};
采用map、hash map用于查找(针对查找元素是否存在的问题,采用map)
class Solution {
public:
string tolow(string s){
for (int i=0;i<s.size();i++){
if (s[i]>='A'&&s[i]<='Z'){
s[i] = s[i]-'A'+'a';
}
}
return s;
}
string removevowel(string s){
string ss = tolow(s);
for(int i=0;i<ss.size();i++){
if (ss[i]=='a'||ss[i]=='e'||ss[i]=='i'||ss[i]=='o'||ss[i]=='u'){
ss[i]='#';
}
}
return ss;
}
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {
map<string, string> map1;
for (int i=0;i<wordlist.size();i++){
if (map1.find(wordlist[i])==map1.end()){
map1[wordlist[i]] = wordlist[i];
}
}
map<string, string> map2;
for (int i=0;i<wordlist.size();i++){
string temp = tolow(wordlist[i]);
if (map2.find(temp)==map2.end()){
map2[temp] = wordlist[i];
}
}
map<string, string> map3;
for (int i=0;i<wordlist.size();i++){
string temp = removevowel(wordlist[i]);
if (map3.find(temp)==map3.end()){
map3[temp] = wordlist[i];
}
}
vector<string> res;
for (int i=0;i<queries.size();i++){
string query = queries[i];
string temp1 = tolow(query);
string temp2 = removevowel(query);
if (map1.find(query)!=map1.end()){
res.push_back(map1[query]);
}
else if (map2.find(temp1)!=map2.end()){
res.push_back(map2[temp1]);
}
else if (map3.find(temp2)!=map3.end()){
res.push_back(map3[temp2]);
}
else{
res.push_back("");
}
}
return res;
}
};