An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations:
a) it --> it (no abbreviation) 1 b) d|o|g --> d1g 1 1 1 1---5----0----5--8 c) i|nternationalizatio|n --> i18n 1 1---5----0 d) l|ocalizatio|n --> l10n
Assume you have a dictionary and given a word, find whether its abbreviation is unique in the dictionary. A word's abbreviation is unique if no other word from the dictionary has the same abbreviation.
Example:
Given dictionary = [ "deer", "door", "cake", "card" ] isUnique("dear") ->false
isUnique("cart") ->true
isUnique("cane") ->false
isUnique("make") ->true
Analyse: no other word from the dictionary has the same abbreviation means that if the dictionary includes that word, and the abbreviation of that word exist only once, then we should return true. hash_map + set
Analyse: hash_map, map the abbreviated word to its original word. If the the abbreviated word already exist in the hash map, and the value of it does not equals to the word itself, set it as empty. Otherwise, map the abbreviated and the original word.
Runtime: 177ms
1 class ValidWordAbbr { 2 public: 3 ValidWordAbbr(vector<string> &dictionary) { 4 for (string word : dictionary) { 5 string abbreviatedWord = getAbbreviate(word); 6 // more than two words map to a single abbreviate word 7 if (abbreviations[abbreviatedWord] != "" && abbreviations[abbreviatedWord] != word) 8 abbreviations[abbreviatedWord] = ""; 9 else 10 abbreviations[abbreviatedWord] = word; 11 } 12 } 13 14 bool isUnique(string word) { 15 string abbreviatedWord = getAbbreviate(word); 16 return abbreviations.count(abbreviatedWord) == 0 || abbreviations[abbreviatedWord] == word; 17 } 18 19 private: 20 unordered_map<string, string> abbreviations; 21 22 string getAbbreviate(string word) { 23 int n = word.size(); 24 if (n < 3) return word; 25 else return word[0] + to_string(n - 2) + word[n - 1]; 26 } 27 }; 28 29 30 // Your ValidWordAbbr object will be instantiated and called as such: 31 // ValidWordAbbr vwa(dictionary); 32 // vwa.isUnique("hello"); 33 // vwa.isUnique("anotherWord");
Analyse: hash_map + set
Runtime: 255ms
1 class ValidWordAbbr { 2 public: 3 ValidWordAbbr(vector<string> &dictionary) { 4 for (string word : dictionary) 5 abbreviateAndOriginal[getAbbreviate(word)].insert(word); 6 } 7 8 bool isUnique(string word) { 9 unordered_set<string> mapToMe = abbreviateAndOriginal[getAbbreviate(word)]; 10 if (mapToMe.empty()) return true; 11 return mapToMe.size() == 1 && mapToMe.count(word) == 1; 12 } 13 private: 14 unordered_map<string, unordered_set<string> > abbreviateAndOriginal; 15 16 string getAbbreviate(string word) { 17 int n = word.size(); 18 if (n < 3) return word; 19 else 20 return word[0] + to_string(n - 2) + word[n - 1]; 21 } 22 };
Analyse: two hash_map
Runtime: 233ms
1 class ValidWordAbbr { 2 public: 3 ValidWordAbbr(vector<string> &dictionary) { 4 for (string word : dictionary) { 5 if (!wordExist[word]) 6 abbreviations[getAbbreviation(word)]++; 7 wordExist[word] = true; 8 } 9 } 10 11 bool isUnique(string word) { 12 return (wordExist[word] && abbreviations[getAbbreviation(word)] == 1) || 13 (!wordExist[word] && !abbreviations[getAbbreviation(word)]); 14 } 15 private: 16 unordered_map<string, bool> wordExist; 17 unordered_map<string, int> abbreviations; 18 19 string getAbbreviation(string word) { 20 int n = word.size(); 21 if (n < 3) return word; 22 else return word[0] + to_string(n - 2) + word[n - 1]; 23 } 24 };
Analyse: hash_map + vector
Runtime: 295ms
1 class ValidWordAbbr { 2 public: 3 ValidWordAbbr(vector<string> &dictionary) { 4 for (string word : dictionary) { 5 string findMe = getAbbreviate(word); 6 if (find(abbreviations[findMe].begin(), abbreviations[findMe].end(), word) == abbreviations[findMe].end()) 7 abbreviations[findMe].push_back(word); 8 } 9 } 10 11 bool isUnique(string word) { 12 vector<string> words = abbreviations[getAbbreviate(word)]; 13 return words.empty() || 14 (words.size() == 1 && words[0] == word); 15 16 } 17 18 private: 19 unordered_map<string, vector<string> > abbreviations; 20 21 string getAbbreviate(string word) { 22 int n = word.size(); 23 if (n < 3) return word; 24 else return word[0] + to_string(n - 2) + word[n - 1]; 25 } 26 };