-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShort Encoding of Words.java
58 lines (51 loc) · 1.66 KB
/
Short Encoding of Words.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
50
51
52
53
54
55
56
57
58
//solution1 remove prefix
class Solution {
public int minimumLengthEncoding(String[] words) {
if (words == null || words.length == 0) return 0;
HashSet<String> set = new HashSet<>();
for (String word : words) {
set.add(word);
}
for (String word : words) {
for (int i = 1; i < word.length(); i++) {
set.remove(word.substring(i));
}
}
int res = 0;
for (String s : set) {
res += s.length() + 1;
}
return res;
}
}
//solution2 trie
class Solution {
public int minimumLengthEncoding(String[] words) {
if (words == null || words.length == 0) return 0;
int res = 1;
TrieNode root = new TrieNode();
for (String word : words) {
TrieNode p = root;
boolean newWord = false;
for (int i = word.length() - 1; i >= 0; i--) {
if (p.isEnd && !newWord) res -= (word.length() - i); // if we visit a old word which is the prefix of this new word, we need to subtract it from answer
if (p.next[word.charAt(i) - 'a'] == null) { // expand what we have in the trie
p.isEnd = false;
newWord = true;
p.next[word.charAt(i) - 'a'] = new TrieNode();
}
p = p.next[word.charAt(i) - 'a'];
}
if (newWord) res += word.length() + 1;
}
return res;
}
class TrieNode {
TrieNode[] next;
boolean isEnd;
public TrieNode() {
next = new TrieNode[26];
isEnd = true;
}
}
}