-
-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathLongestWord.java
78 lines (65 loc) · 2.12 KB
/
LongestWord.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
package com.leetcode.trie;
import java.util.HashMap;
import java.util.Stack;
/**
* Level: Easy
* Problem: https://leetcode.com/problems/longest-word-in-dictionary/
*
* @author rampatra
* @since 2019-04-10
*/
public class LongestWord {
private class TrieNode {
char ch;
HashMap<Character, TrieNode> children = new HashMap<>();
String completeWord; // to mark a complete word in the trie data structure
TrieNode(char ch) {
this.ch = ch;
}
}
private TrieNode root = new TrieNode('0');
/**
* Inserts {@code data} in trie.
*
* @param str
*/
public void insert(String str) {
char c;
TrieNode curr = root;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
curr.children.putIfAbsent(c, new TrieNode(c));
curr = curr.children.get(c);
}
curr.completeWord = str;
}
public String longestWord(String[] words) {
for (int i = 0; i < words.length; i++) {
insert(words[i]);
}
return longestWord();
}
private String longestWord() {
String longestWord = "";
TrieNode curr;
Stack<TrieNode> stack = new Stack<>();
stack.addAll(root.children.values());
while (!stack.empty()) {
curr = stack.pop();
if (curr.completeWord != null) {
if (curr.completeWord.length() > longestWord.length() ||
(curr.completeWord.length() == longestWord.length() &&
curr.completeWord.compareTo(longestWord) < 0)) {
longestWord = curr.completeWord;
}
stack.addAll(curr.children.values());
}
}
return longestWord;
}
public static void main(String[] args) {
LongestWord longestWord = new LongestWord();
System.out.println(longestWord.longestWord(new String[]{"w", "wo", "wor", "worl", "world"}));
System.out.println(longestWord.longestWord(new String[]{"a", "banana", "app", "appl", "ap", "apply", "apple"}));
}
}