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
Attention
DIC = ["Hello"] word = "Hello" isunique
Public classvalidwordabbr {HashMap<string, string>set; Publicvalidwordabbr (string[] dictionary) {set=NewHashmap<string, string>(); for(inti = 0; i < dictionary.length; i++) {String temp=Dictionary[i]; if(Set.containskey (GetKey (temp)) &&!set.get (GetKey (temp)). Equals (temp)) Set.put (GetKey ( temp),""); Elseset.put (GetKey (temp), temp); } } Publicstring GetKey (string s) {if(S.length () <= 2)returns; Else{ returnS.charat (0) +integer.tostring (s.length ()-2) +s.charat (S.length ()-1); } } Public BooleanIsUnique (String word) {if(Set.containskey (GetKey (Word))) {returnSet.get (GetKey (Word)). Equals (word); } Else return true; }}//Your Validwordabbr object would be instantiated and called as such://validwordabbr VWA = new Validwordabbr (dictionary);//vwa.isunique ("Word");//vwa.isunique ("Anotherword");
288. Unique Word abbreviation