forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
49 lines (46 loc) · 1.39 KB
/
Solution.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
40
41
42
43
44
45
46
47
48
49
class Trie {
private final Trie[] children = new Trie[26];
private int cnt;
public void insert(String w) {
Trie node = this;
for (char c : w.toCharArray()) {
int idx = c - 'a';
if (node.children[idx] == null) {
node.children[idx] = new Trie();
}
node = node.children[idx];
++node.cnt;
}
}
public int search(String w) {
Trie node = this;
int ans = 0;
for (char c : w.toCharArray()) {
++ans;
int idx = c - 'a';
node = node.children[idx];
if (node.cnt == 1) {
return ans;
}
}
return w.length();
}
}
class Solution {
public List<String> wordsAbbreviation(List<String> words) {
Map<List<Integer>, Trie> tries = new HashMap<>();
for (var w : words) {
var key = List.of(w.length(), w.charAt(w.length() - 1) - 'a');
tries.putIfAbsent(key, new Trie());
tries.get(key).insert(w);
}
List<String> ans = new ArrayList<>();
for (var w : words) {
int m = w.length();
var key = List.of(m, w.charAt(m - 1) - 'a');
int cnt = tries.get(key).search(w);
ans.add(cnt + 2 >= m ? w : w.substring(0, cnt) + (m - cnt - 1) + w.substring(m - 1));
}
return ans;
}
}