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
Show Company TagsShow TagsShow Similar Problems
1 classVALIDWORDABBR {2 Private:3unordered_map<string,int>dict;4unordered_set<string>indict;5 stringGETABBR (stringword) {6 if(Word.size () <3)returnWord;7 returnWord.front () + std::to_string (word.size ()-2) +Word.back ();8 }9 Public:TenVALIDWORDABBR (vector<string> &dictionary) { One for(inti =0, n = dictionary.size (); I < n; i++) { A if(Indict.count (Dictionary[i]))Continue; - Indict.insert (Dictionary[i]); - stringabbr =getabbr (Dictionary[i]); the if(Dict.count (abbr) = =0) -Dict.insert (Make_pair (abbr,1)); - Elsedict[abbr]++; - } + } - + BOOLIsUnique (stringword) { A stringabbr =GETABBR (word); at if(Dict.count (abbr) = =0|| (DICT[ABBR] = =1&& Indict.count (word) = =1)) - return true; - return false; - } - }; - in - //Your Validwordabbr object would be instantiated and called as such: to //validwordabbr VWA (dictionary); + //vwa.isunique ("Hello"); - //vwa.isunique ("Anotherword");
Important: int to string can be used with function std::to_string ()
The role of indict is two: 1) the dictionary to be given to the weight. 2) Determine if the word to be detected is in the given dictionary.
Unique Word abbreviation--Leetcode