Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e34600d

Browse files
committed
Improved trie code + leetcode longest word done
1 parent 8363578 commit e34600d

File tree

2 files changed

+94
-28
lines changed

2 files changed

+94
-28
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.leetcode.trie;
2+
3+
import java.util.HashMap;
4+
import java.util.Stack;
5+
6+
/**
7+
* Leetcode Problem: https://leetcode.com/problems/longest-word-in-dictionary/
8+
*
9+
* @author rampatra
10+
* @since 2019年04月10日
11+
*/
12+
public class LongestWord {
13+
14+
private class TrieNode {
15+
char ch;
16+
HashMap<Character, TrieNode> children = new HashMap<>();
17+
String completeWord; // to mark a complete word in the tri data structure
18+
19+
TrieNode(char ch) {
20+
this.ch = ch;
21+
}
22+
}
23+
24+
private TrieNode root = new TrieNode('0');
25+
26+
/**
27+
* Inserts {@code data} in trie.
28+
*
29+
* @param str
30+
*/
31+
public void insert(String str) {
32+
char c;
33+
TrieNode curr = root;
34+
35+
for (int i = 0; i < str.length(); i++) {
36+
c = str.charAt(i);
37+
curr.children.putIfAbsent(c, new TrieNode(c));
38+
curr = curr.children.get(c);
39+
}
40+
41+
curr.completeWord = str;
42+
}
43+
44+
public String longestWord(String[] words) {
45+
for (int i = 0; i < words.length; i++) {
46+
insert(words[i]);
47+
}
48+
49+
return longestWord();
50+
}
51+
52+
private String longestWord() {
53+
String longestWord = "";
54+
TrieNode curr;
55+
Stack<TrieNode> stack = new Stack<>();
56+
stack.addAll(root.children.values());
57+
58+
while (!stack.empty()) {
59+
curr = stack.pop();
60+
if (curr.completeWord != null) {
61+
if (curr.completeWord.length() > longestWord.length() ||
62+
(curr.completeWord.length() == longestWord.length() &&
63+
curr.completeWord.compareTo(longestWord) < 0)) {
64+
longestWord = curr.completeWord;
65+
}
66+
stack.addAll(curr.children.values());
67+
}
68+
}
69+
return longestWord;
70+
}
71+
72+
public static void main(String[] args) {
73+
LongestWord longestWord = new LongestWord();
74+
System.out.println(longestWord.longestWord(new String[]{"w", "wo", "wor", "worl", "world"}));
75+
System.out.println(longestWord.longestWord(new String[]{"a", "banana", "app", "appl", "ap", "apply", "apple"}));
76+
}
77+
}

‎src/main/java/com/rampatra/base/Trie.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,37 @@
1919
* @author rampatra
2020
* @since 9/22/15
2121
*/
22-
public class Trie<E> {
22+
public class Trie {
2323

2424
private class TrieNode<T> {
25-
HashMap<T, TrieNode<T>> children;
25+
char ch;
26+
HashMap<T, TrieNode<T>> children = new HashMap<>();
2627
boolean isCompleteWord; // to mark a complete word in the tri data structure
2728

28-
TrieNode(HashMap<T, TrieNode<T>> children) {
29-
this.children = children;
29+
TrieNode(charch) {
30+
this.ch = ch;
3031
}
3132
}
3233

3334
private TrieNode<Character> root;
3435

3536
Trie() {
36-
root = new TrieNode<>(newHashMap<>());
37+
root = new TrieNode<>('0');
3738
}
3839

3940
/**
4041
* Inserts {@code data} in trie.
4142
*
42-
* @param data
43+
* @param str
4344
*/
44-
public void insert(E data) {
45-
46-
int i = 0;
47-
String str = data.toString();
45+
public void insert(String str) {
46+
char c;
4847
TrieNode<Character> curr = root;
4948

50-
while (i < str.length()) {
51-
if (curr.children.get(str.charAt(i)) != null) {
52-
curr = curr.children.get(str.charAt(i));
53-
i++;
54-
} else {
55-
break;
56-
}
57-
}
58-
59-
while (i < str.length()) {
60-
curr.children.put(str.charAt(i), new TrieNode<>(new HashMap<>()));
61-
curr = curr.children.get(str.charAt(i));
62-
i++;
49+
for (int i = 0; i < str.length(); i++) {
50+
c = str.charAt(i);
51+
curr.children.putIfAbsent(c, new TrieNode<>(c));
52+
curr = curr.children.get(c);
6353
}
6454

6555
curr.isCompleteWord = true;
@@ -68,12 +58,11 @@ public void insert(E data) {
6858
/**
6959
* Searches {@code data} in trie.
7060
*
71-
* @param data the value to search.
72-
* @return {@code true} if {@code data} is present, {@code false} otherwise.
61+
* @param str the value to search.
62+
* @return {@code true} if {@code str} is present, {@code false} otherwise.
7363
*/
74-
public boolean search(Edata) {
64+
public boolean search(Stringstr) {
7565

76-
String str = data.toString();
7766
TrieNode<Character> curr = root;
7867

7968
for (int i = 0; i < str.length(); i++) {
@@ -88,7 +77,7 @@ public boolean search(E data) {
8877

8978
// unit testing
9079
public static void main(String[] args) {
91-
Trie<String> trie = new Trie<>();
80+
Trie trie = new Trie();
9281
trie.insert("ram");
9382
trie.insert("r");
9483
trie.insert("rama");

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /