# 288. Unique Word Abbreviation

An abbreviation of a word follows the form . Below are some examples of word abbreviations:

a) it --> it (no abbreviation)

`````` 1
↓
``````

b) d|o|g --> d1g

``````          1    1  1
1---5----0----5--8
↓   ↓    ↓    ↓  ↓
``````

c) i|nternationalizatio|n --> i18n

``````          1
1---5----0
↓   ↓    ↓
``````

d) 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 no other word from the dictionary has the same abbreviation.

Example:

Given dictionary = [ "deer", "door", "cake", "card" ]

isUnique("dear") -> false
isUnique("cart") -> true
isUnique("cane") -> false
isUnique("make") -> true

## Solution

choose the right data structure

there exits multiple situations:
1. abbreviation not in dict -> true
2. abbreviation belongs to one word but appear multiple times -> true
3. abbreviation belongs to multiple word -> false

## Code

``````class ValidWordAbbr {
private Map<String, Map<String, Integer>> map = new HashMap<>();
public ValidWordAbbr(String[] dictionary) {
for(String word : dictionary){
String key = getKey(word);
if(!map.containsKey(key)) map.put(key, new HashMap<>());
Map<String, Integer> subMap = map.get(key);
subMap.put(word, subMap.getOrDefault(word, 0) + 1);
}
}

public boolean isUnique(String word) {
String key = getKey(word);
if(!map.containsKey(key)) return true;
if(map.get(key).size() == 1 && map.get(key).containsKey(word))
return true;
return false;
}

private String getKey(String word){
String key = "";
if(word.length() <= 2) key = word;
else{
StringBuilder sb = new StringBuilder();
sb.append(word.charAt(0)).append(word.length() - 2).append(word.charAt(word.length() -1));
key = sb.toString();
}
return key;
}
}

/**
* Your ValidWordAbbr object will be instantiated and called as such:
* ValidWordAbbr obj = new ValidWordAbbr(dictionary);
* boolean param_1 = obj.isUnique(word);
*/
``````