Skip to content

Commit 6284bb2

Browse files
committed
Longest word dictionary
1 parent 8b1a51f commit 6284bb2

File tree

4 files changed

+75
-25
lines changed

4 files changed

+75
-25
lines changed

src/leetcode/MergeTwoSortedLists.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package leetcode;
22

3-
class ListNode {
3+
/*class ListNode {
44
int val;
55
ListNode next;
66
ListNode(int x) {
77
val = x;
88
next=null;
99
}
10-
}
10+
}*/
1111

1212
public class MergeTwoSortedLists {
1313

src/leetcode/NextGreaterElementII.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/leetcode/SortListTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public ListNode merge(ListNode l1, ListNode l2){
5151

5252
}
5353

54-
class ListNode{
54+
/*class ListNode{
5555
int val;
5656
ListNode next;
5757
ListNode(int x){
5858
val=x;
5959
}
60-
}
60+
}*/
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package leetcode2018;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class LongestWordDictionary {
7+
public String longestWord(String[] words) {
8+
TrieNode root = new TrieNode('0');
9+
Result re = new Result("");
10+
for(String word: words)
11+
insert(word, root);
12+
dfs(root, re);
13+
return re.val;
14+
}
15+
16+
public void dfs(TrieNode root, Result re){
17+
if(root.isComplete && re.length < root.word.length()){
18+
re.val = root.word;
19+
re.length=root.word.length();
20+
}
21+
if(root.isComplete && root.word.length() == re.length && root.word.compareTo(re.val)<0){
22+
re.val = root.word;
23+
re.length=root.word.length();
24+
}
25+
for(char c: root.children.keySet()){
26+
if(root.children.get(c).isComplete)
27+
dfs(root.children.get(c), re);
28+
}
29+
}
30+
31+
public void insert(String word, TrieNode root){
32+
TrieNode currentRoot = root;
33+
int i =0;
34+
for(char c: word.toCharArray()){
35+
if(!currentRoot.children.containsKey(c)){
36+
TrieNode node = new TrieNode(c);
37+
currentRoot.children.putIfAbsent(c, node);
38+
}
39+
40+
currentRoot = currentRoot.children.get(c);
41+
i++;
42+
if(word.length()==i){
43+
currentRoot.isComplete = true;
44+
currentRoot.word = word;
45+
}
46+
47+
}
48+
}
49+
50+
class Result{
51+
String val;
52+
int length;
53+
Result(String val){
54+
this.val= val;
55+
this.length=0;
56+
}
57+
}
58+
59+
class TrieNode {
60+
Character val;
61+
Map<Character, TrieNode> children;
62+
String word;
63+
boolean isComplete;
64+
TrieNode(Character val){
65+
this.val = val;
66+
this.children = new HashMap<>();
67+
this.word = "";
68+
this.isComplete = false;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy