-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestWordInDictionary.java
104 lines (82 loc) · 2.79 KB
/
LongestWordInDictionary.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author Lillard
*/
public class LongestWordInDictionary {
public static class Solution {
public Solution() {
}
private Node answer;
private class Node {
private char value;
private boolean mark;
private int dep;
private Node pre;
private Map<Character, Node> nextNode;
private Node(char value, int dep, Node pre) {
this.value = value;
this.mark = false;
this.dep = dep;
this.nextNode = new HashMap<>();
this.pre = pre;
}
public void buildTree(String word) {
if (word == null || word.length() <= 0) {
return;
}
this.buildTree(word, 0);
}
public void buildTree(String word, int index) {
if (word.length() == index) {
this.mark = true;
return;
}
char value = word.charAt(index);
Node next = this.nextNode.computeIfAbsent(value, key -> new Node(value, this.dep + 1, this));
next.buildTree(word, index + 1);
}
public void dfs() {
if (!this.mark) {
return;
}
if (answer.dep < this.dep) {
answer = this;
}
for (char c = 'a'; c <= 'z'; c++) {
final Node next = this.nextNode.get(c);
if (next != null) {
next.dfs();
}
}
}
public void buildAnswer(StringBuffer buffer) {
if (this.dep == 0) {
return;
}
this.pre.buildAnswer(buffer);
buffer.append(this.value);
}
}
public String longestWord(String[] words) {
Node root = new Node('0', 0, null);
root.mark = true;
Arrays.stream(words).forEach(root::buildTree);
answer = root;
root.dfs();
if (answer == root) {
return "";
} else {
StringBuffer buffer = new StringBuffer();
answer.buildAnswer(buffer);
return buffer.toString();
}
}
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.longestWord(new String[]{"w","wo","wor","worl", "world"}));
System.out.println(solution.longestWord(new String[]{"a", "banana", "app", "appl", "ap", "apply", "apple"}));
}
}