288. Unique Word Abbreviation

An abbreviation of a word follows the form . Below are some examples of word abbreviations:
a) it                      --> it    (no abbreviation)
b) l|ocalizatio|n          --> l10n
Assume you have a dictionary and given a word, find whether its abbreviation is unique in the dictionary. A word's abbreviation is unique if nootherword from the dictionary has the same abbreviation.

Example:

Given dictionary = [ "deer", "door", "cake", "card" ]
isUnique("dear") ->false
isUnique("cart") ->true
isUnique("cake") ->true
isUnique("make") ->true

这个题用hashmap 很简单, 但是要注意一点, 如果dictionary 里有个cake, 唯一一个c2e 那么, 问cake的时候要返回true, 所以说 要把抽象字符, 和字符 存储起来, 如果字符唯一, 那么要存起来, 字符不唯一, 要把里面刚刚存的用空串清除掉

if(map.containsKey(key) && !map.get(key).equals(str)){
      map.put(key, "");
}

这段代码是关键!

288. Unique Word Abbreviation_第1张图片

你可能感兴趣的:(288. Unique Word Abbreviation)