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)

     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

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 };
时间: 2024-10-21 00:30:57

Unique Word Abbreviation的相关文章

[Locked] 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) 1 b) d|o|g --> d1g 1 1 1 1---5----0----5--8 c) i

288. 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) 1 b) d|o|g --> d1g 1 1 1 1---5----0----5--8 c) i|nternationalizatio|n

Leetcode: 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) 1 b) d|o|g --> d1g 1 1 1 1---5----0----5--8 c) i|nternationalizatio|n --&

Unique Word Abbreviation -- LeetCode

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 --&

[LeetCode] 288.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) 1 b) d|o|g --> d1g 1 1 1 1---5----0----5--8 c) i|nternationalizatio|n --&

411. Minimum Unique Word Abbreviation

A string such as "word" contains the following abbreviations: ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "

Leetcode: Minimum Unique Word Abbreviation

A string such as "word" contains the following abbreviations: ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "

[LeetCode] Valid Word Abbreviation 验证单词缩写

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &

[LeetCode] 527. Word Abbreviation 单词缩写

Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below. Begin with the first character and then the number of characters abbreviated, which followed by the last charact