288 - Unique Word Abbreviation
Written on October 29, 2015
Tweet
**An abbreviation of a word follows the form
public class ValidWordAbbr {
private HashMap<String, String> map = new HashMap<String, String>();
public ValidWordAbbr(String[] dictionary) {
if (dictionary == null || dictionary.length == 0) return;
for (String s : dictionary) {
if (s.length() <= 2) continue;
String newS = s.charAt(0) + String.valueOf(s.length() - 2) + s.charAt(s.length() - 1);
if (map.containsKey(newS)) {
map.put(newS, "");
} else {
map.put(newS, s);
}
}
}
public boolean isUnique(String word) {
if (word == null || word.length() <= 2) return true;
String newS = word.charAt(0) + String.valueOf(word.length() - 2) + word.charAt(word.length() - 1);
return !map.containsKey(newS) || map.get(newS).equals(word);
}
}
class ValidWordAbbr(object):
def __init__(self, dictionary):
"""
:type dictionary: List[str]
"""
self.word_map = {}
for s in dictionary:
if len(s) <= 2:
continue
abbr = s[0] + str(len(s) - 2) + s[-1]
if abbr in self.word_map:
self.word_map[abbr] = ""
else:
self.word_map[abbr] = s
def isUnique(self, word):
"""
:type word: str
:rtype: bool
"""
if len(word) <= 2:
return True
abbr = word[0] + str(len(word) - 2) + word[-1]
return word == self.word_map.get(abbr, word)