首页 > 代码库 > Unique Word Abbreviation

Unique Word Abbreviation

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)     1b) d|o|g                   --> d1g              1    1  1     1---5----0----5--8c) i|nternationalizatio|n  --> i18n              1     1---5----0d) 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") -> falseisUnique("cart") -> trueisUnique("cane") -> falseisUnique("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

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 };

 

Unique Word Abbreviation