forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidWordAbbr.java
39 lines (34 loc) · 1.12 KB
/
ValidWordAbbr.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
32
33
34
35
36
37
38
39
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
/**
* 参考https://leetcode.com/articles/unique-word-abbreviation/
*/
public class ValidWordAbbr {
/**
* 这题容易错的地方是unique的理解,即dic中不存在该word,要排除word
*/
private HashMap<String, Set<String>> mMap;
public ValidWordAbbr(String[] dictionary) {
mMap = new HashMap<String, Set<String>>();
for (String text : dictionary) {
String abbr = getAbbr(text);
Set<String> set = mMap.get(abbr);
if (set == null) {
set = new HashSet<String>();
mMap.put(abbr, set);
}
set.add(text);
}
}
/**
* unique意味着不存在该abbr或存在但是只有该单词一个
*/
public boolean isUnique(String word) {
Set<String> set = mMap.get(getAbbr(word));
return set == null || (set.size() == 1 && set.contains(word));
}
private String getAbbr(String s) {
return s.length() > 2 ? s.substring(0, 1) + (s.length() - 2) + s.substring(s.length() - 1) : s;
}
}