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--d1g1 1 1 1---5----0----5--8c) I|nternationalizatio|n--i18n1 1---5----0d) L|ocalizatio|n--L10nassume 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 other word from the dictionary have the same abbreviation.Example:given Dictionary= ["Deer", "door", "Cake", "card"]isunique ("Dear")falseIsUnique ("Cart")trueIsUnique ("Cane")falseIsUnique ("Make")true
1 Public classVALIDWORDABBR {2 string[] dict;3Hashmap<string, set<string>>Afterabbr;4 5 Publicvalidwordabbr (string[] dictionary) {6 This. Dict =dictionary;7 This. Afterabbr =NewHashmap<string, set<string>>();8 for(String item:dictionary) {9String str =abbr (item);Ten if(Afterabbr.containskey (str)) { One afterabbr.get (str). Add (item); A } - Else { -Hashset<string> set =NewHashset<string>(); the Set.add (item); - afterabbr.put (str, set); - } - } + } - + Public BooleanIsUnique (String word) { AString str =abbr (word); at if(!afterabbr.containskey (str))return true; - Else if(Afterabbr.containskey (str) && afterabbr.get (str). Contains (word) && afterabbr.get (str). Size () ==1)return true; - return false; - } - - Publicstring abbr (String item) { in if(Item = =NULL)return NULL; - if(Item.length () <= 2)returnitem; toStringBuffer res =NewStringBuffer (); +Res.append (Item.charat (0)); -Res.append (Item.length ()-2); theRes.append (Item.charat (Item.length ()-1)); * returnres.tostring (); $ }Panax Notoginseng } - the + //Your Validwordabbr object would be instantiated and called as such: A //validwordabbr VWA = new Validwordabbr (dictionary); the //vwa.isunique ("Word"); + //vwa.isunique ("Anotherword");
Leetcode:unique Word abbreviation