|
| 1 | +class Solution { |
| 2 | +public: |
| 3 | + bool cons(char c) //returns true if character is a non-vowel |
| 4 | + { |
| 5 | + if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u') |
| 6 | + return true; |
| 7 | + return false; |
| 8 | + } |
| 9 | + string modify(string s) |
| 10 | + { |
| 11 | + string ans=""; |
| 12 | + for(int i=0;i<=s.size();i++) |
| 13 | + { |
| 14 | + if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u'){ |
| 15 | + ans+="$"; |
| 16 | + }else{ |
| 17 | + ans+=s[i]; |
| 18 | + } |
| 19 | + } |
| 20 | + return ans; |
| 21 | + } |
| 22 | + vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) { |
| 23 | + |
| 24 | + int n = wordlist.size(); |
| 25 | + |
| 26 | + map<string,int> mp_exact; |
| 27 | + map<string,int> mp_lower; |
| 28 | + map<string,int> mp_modified; |
| 29 | + vector<string> lower(n); |
| 30 | + for(int i=0;i< n;i++) |
| 31 | + { |
| 32 | + string s = wordlist[i]; |
| 33 | + mp_exact[s]=i+1; |
| 34 | + |
| 35 | + //mp_exact hashmap will store index+1 of occurence of word from wordList |
| 36 | + |
| 37 | + transform(s.begin(),s.end(),s.begin(),::tolower); |
| 38 | + lower[i]=s; |
| 39 | + if(!mp_lower[s]){ |
| 40 | + mp_lower[s]=i+1; //we store index+1 |
| 41 | + } |
| 42 | + |
| 43 | + string modified = modify(s); |
| 44 | + if(!mp_modified[modified]){ |
| 45 | + mp_modified[modified]=i+1; |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + int m = queries.size(); |
| 51 | + vector<string> ans; |
| 52 | + for(int i=0;i<m;i++) |
| 53 | + { |
| 54 | + string q = queries[i]; |
| 55 | + if(mp_exact[q]){ // when we get exact match |
| 56 | + ans.push_back(q); |
| 57 | + }else{ |
| 58 | + transform(q.begin(),q.end(),q.begin(),::tolower); |
| 59 | + //we have converted query to all lower now |
| 60 | + |
| 61 | + if(mp_lower[q]) //after converting query to lower and checking in lower map if we get match |
| 62 | + { |
| 63 | + int ind = mp_lower[q]-1; |
| 64 | + ans.push_back(wordlist[ind]); |
| 65 | + }else{ |
| 66 | + //now we check for vowel mismatch case |
| 67 | + |
| 68 | + string q_mod = modify(q); |
| 69 | + //modified query such that all vowels are $ -> only non-vowel remain |
| 70 | + |
| 71 | + if(mp_modified[q_mod]) |
| 72 | + { |
| 73 | + int ind = mp_modified[q_mod]-1; |
| 74 | + ans.push_back(wordlist[ind]); |
| 75 | + }else{ |
| 76 | + ans.push_back(""); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return ans; |
| 83 | + |
| 84 | + |
| 85 | + } |
| 86 | +}; |
0 commit comments