Problem Description:
An abbreviation of a word follows the form <first letter><number><last letter>. Below is 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 has a dictionary and given a word, find whether its abbreviation are unique in the dictionary. A word ' s abbreviation is A unique if No and Word from the dictionary have the same abbreviation.
Example:
false
true
false
true
To check for unique abbreviation, we maintain a mapping from a specific abbreviation to all words which has the Abbreviat Ion. Then we just need to check no other words has the same abbreviation as the given word.
The code is as follows.
classVALIDWORDABBR { Public: validwordabbr (Vector<string> &dictionary) { for(string&d:dictionary) { intn =d.length (); stringabbr = d[0] + to_string (n) + d[n-1]; Mp[abbr].insert (d); } } BOOLIsUnique (stringword) { intn =word.length (); stringabbr = word[0] + to_string (n) + word[n-1]; returnMp[abbr].count (word) = =mp[abbr].size (); }Private: Unordered_map<string, unordered_set<string>>MP;};//Your Validwordabbr object would be instantiated and called as such://validwordabbr VWA (dictionary);//vwa.isunique ("Hello");//vwa.isunique ("Anotherword");
[Leetcode] Unique Word abbreviation