-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUniqueWordAbbreviation.java
More file actions
34 lines (29 loc) · 1.07 KB
/
UniqueWordAbbreviation.java
File metadata and controls
34 lines (29 loc) · 1.07 KB
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
32
33
34
public class ValidWordAbbr {
private Map<String, List<String>> map;
public String getAbbr(String s) {
if (s.length() < 3) return s;
else return "" + s.charAt(0) + (s.length() - 2) + s.charAt(s.length() - 1);
}
public ValidWordAbbr(String[] dictionary) {
map = new HashMap<String, List<String>>();
for (String word : dictionary) {
if (word.length() > 2) {
String abbr = getAbbr(word);
if (!map.containsKey(abbr)) {
map.put(abbr, new ArrayList<String>(Arrays.asList(word)));
} else {
map.get(abbr).add(word);
}
}
}
}
public boolean isUnique(String word) {
String abbr = getAbbr(word);
return !map.containsKey(abbr) ||
(map.get(abbr).size() == 1 && map.get(abbr).get(0).equals(word));
}
}
// Your ValidWordAbbr object will be instantiated and called as such:
// ValidWordAbbr vwa = new ValidWordAbbr(dictionary);
// vwa.isUnique("Word");
// vwa.isUnique("anotherWord");