-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode288.java
31 lines (25 loc) · 921 Bytes
/
leetcode288.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class ValidWordAbbr {
Map<String, String> map = new HashMap<>();
public ValidWordAbbr(String[] dictionary) {
for(String word : dictionary){
String wordAbbr = getAbbreviation(word);
if(map.containsKey(wordAbbr) && !map.get(wordAbbr).equals(word))
map.put(wordAbbr,"");
else
map.put(wordAbbr, word);
}
}
public boolean isUnique(String word) {
return !map.containsKey(getAbbreviation(word))||map.get(getAbbreviation(word)).equals(word);
}
private String getAbbreviation(String s){
if(s.length() <= 2)
return s;
String abbr = s.charAt(0) + Integer.toString(s.length()-2) + s.charAt(s.length()-1);
return abbr;
}
}
// Your ValidWordAbbr object will be instantiated and called as such:
// ValidWordAbbr vwa = new ValidWordAbbr(dictionary);
// vwa.isUnique("Word");
// vwa.isUnique("anotherWord");