Skip to content

Commit a0564f2

Browse files
committed
Runtime: 23 ms (Top 85.71%) | Memory: 59.1 MB (Top 35.19%)
1 parent 3a6d82b commit a0564f2

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,69 @@
1+
// Runtime: 23 ms (Top 85.71%) | Memory: 59.1 MB (Top 35.19%)
12
class Node {
23
private boolean flag;
34
private Node[] children;
4-
5+
56
public Node () {
67
flag = false;
78
children = new Node[26];
89
Arrays.fill (children, null);
910
}
10-
11+
1112
public boolean getFlag () {
1213
return flag;
1314
}
14-
15+
1516
public Node getChild (int index) {
1617
return children[index];
1718
}
18-
19+
1920
public boolean hasChild (int index) {
2021
return children[index] != null;
2122
}
22-
23+
2324
public void setFlag (boolean flag) {
2425
this.flag = flag;
2526
}
26-
27+
2728
public void makeChild (int index) {
2829
children[index] = new Node();
2930
}
3031
}
3132

3233
class Trie {
3334
private Node root;
34-
35+
3536
public Trie () {
3637
root = new Node();
3738
}
38-
39+
3940
public int addWord (String word) {
40-
41+
4142
boolean flag = true;
4243
Node node = root;
4344
int count = 0;
44-
45+
4546
for (int i = word.length () - 1; i >= 0; --i) {
4647
int index = (int) word.charAt(i) - 97;
47-
48+
4849
if (!node.hasChild (index)) {
4950
flag = false;
5051
node.makeChild (index);
5152
}
52-
53+
5354
node = node.getChild (index);
5455
if (node.getFlag()) {
5556
node.setFlag (false);
5657
count -= word.length() - i + 1;
57-
58+
5859
if (i == 0)
5960
flag = false;
6061
}
6162
}
62-
63+
6364
if (!flag)
6465
node.setFlag (true);
65-
66+
6667
return flag? count: count + 1 + word.length();
6768
}
6869
}
@@ -71,11 +72,11 @@ class Solution {
7172
public int minimumLengthEncoding(String[] words) {
7273
Trie trie = new Trie ();
7374
int size = 0;
74-
75+
7576
for (String word: words) {
7677
size += trie.addWord (word);
7778
}
78-
79+
7980
return size;
8081
}
81-
}
82+
}

0 commit comments

Comments
 (0)