public class ValidWordAbbr {
private HashMap<String, String> dict;
private String getKey(String str) {
int len = str.length();
if( len <= 2) return str;
StringBuilder sb = new StringBuilder();
sb.append(str.charAt(0)).append(len - 2).append(str.charAt(len - 1));
return sb.toString();
}
public ValidWordAbbr(String[] dictionary) {
dict = new HashMap<String, String>();
for(String word : dictionary) {
String key = getKey(word);
if(!dict.containsKey(key)) {
dict.put(key, word);
} else if(!dict.get(key).equals(word)){
dict.put(key, "");
}
}
}
public boolean isUnique(String word) {
String key = getKey(word);
return !dict.containsKey(key) || dict.get(key).equals(word);
}
}
/**
* Your ValidWordAbbr object will be instantiated and called as such:
* ValidWordAbbr obj = new ValidWordAbbr(dictionary);
* boolean param_1 = obj.isUnique(word);
*/