Unique Word abbreviation
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
1 classVALIDWORDABBR {2 Private:3unordered_map<string,int>M_map;4unordered_set<string>M_set;5 Public:6VALIDWORDABBR (vector<string> &dictionary) {7 for(Auto word:dictionary) {8 M_set.insert (word);9 if(word.length () = =2) ++M_map[word];Ten Else++m_map[word.front () + to_string (Word.length ()-2) +Word.back ()]; One } A } - - BOOLIsUnique (stringword) { the stringkey; - if(word.length () = =2) key =Word; - ElseKey = Word.front () + to_string (Word.length ()-2) +Word.back (); - if(M_set.find (word) = = M_set.end ())returnM_map[key] <1; + Else returnM_map[key] <2; - } + }; A at - //Your Validwordabbr object would be instantiated and called as such: - //validwordabbr VWA (dictionary); - //vwa.isunique ("Hello"); - //vwa.isunique ("Anotherword");
[Leetcode] Unique Word abbreviation