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, "");
}

这段代码是关键!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容