Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions 966. Vowel Spellchecker
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
class Solution {
public:
bool cons(char c) //returns true if character is a non-vowel
{
if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u')
return true;
return false;
}
string modify(string s)
{
string ans="";
for(int i=0;i<=s.size();i++)
{
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u'){
ans+="$";
}else{
ans+=s[i];
}
}
return ans;
}
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {

int n = wordlist.size();

map<string,int> mp_exact;
map<string,int> mp_lower;
map<string,int> mp_modified;
vector<string> lower(n);
for(int i=0;i< n;i++)
{
string s = wordlist[i];
mp_exact[s]=i+1;

//mp_exact hashmap will store index+1 of occurence of word from wordList

transform(s.begin(),s.end(),s.begin(),::tolower);
lower[i]=s;
if(!mp_lower[s]){
mp_lower[s]=i+1; //we store index+1
}

string modified = modify(s);
if(!mp_modified[modified]){
mp_modified[modified]=i+1;
}

}

int m = queries.size();
vector<string> ans;
for(int i=0;i<m;i++)
{
string q = queries[i];
if(mp_exact[q]){ // when we get exact match
ans.push_back(q);
}else{
transform(q.begin(),q.end(),q.begin(),::tolower);
//we have converted query to all lower now

if(mp_lower[q]) //after converting query to lower and checking in lower map if we get match
{
int ind = mp_lower[q]-1;
ans.push_back(wordlist[ind]);
}else{
//now we check for vowel mismatch case

string q_mod = modify(q);
//modified query such that all vowels are $ -> only non-vowel remain

if(mp_modified[q_mod])
{
int ind = mp_modified[q_mod]-1;
ans.push_back(wordlist[ind]);
}else{
ans.push_back("");
}
}
}
}

return ans;


}
};
Loading