Skip to content

Commit 78dd728

Browse files
[N-0] add 720
1 parent 6af47cd commit 78dd728

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|720|[Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_720.java) | O(∑wi) where wi is the length of words[i] | O(∑wi) where wi is the length of words[i] | Easy | Trie
2526
|719|[Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_719.java) | O(nlogw + nlogn) | O(1) | Hard | Binary Search
2627
|718|[Maximum Length of Repeated Subarray](https://leetcode.com/problems/maximum-length-of-repeated-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_718.java) | O(m*n) | O(m*n) | Medium | DP
2728
|717|[1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_717.java) | O(n) | O(1) | Easy |
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 720. Longest Word in Dictionary.
5+
*
6+
* Given a list of strings words representing an English Dictionary,
7+
* find the longest word in words that can be built one character at a time by other words in words.
8+
* If there is more than one possible answer, return the longest word with the smallest lexicographical order.
9+
* If there is no answer, return the empty string.
10+
11+
Example 1:
12+
Input:
13+
words = ["w","wo","wor","worl", "world"]
14+
Output: "world"
15+
Explanation:
16+
The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
17+
18+
Example 2:
19+
Input:
20+
words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
21+
Output: "apple"
22+
Explanation:
23+
Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
24+
25+
Note:
26+
All the strings in the input will only contain lowercase letters.
27+
The length of words will be in the range [1, 1000].
28+
The length of words[i] will be in the range [1, 30].
29+
*/
30+
31+
public class _720 {
32+
public static class Solution1 {
33+
public String longestWord(String[] words) {
34+
TrieNode root = buildTrie(words);
35+
return findLongestWord(root, words);
36+
}
37+
38+
private String findLongestWord(TrieNode root, String[] words) {
39+
String longestWord = "";
40+
for (String word : words) {
41+
if (longestWord.length() > word.length() || (longestWord.length() == word.length() && (longestWord.compareToIgnoreCase(word) < 0))) {
42+
continue;
43+
}
44+
TrieNode tmp = root;
45+
boolean validWord = true;
46+
for (char c : word.toCharArray()) {
47+
if (tmp.children[c - 'a'] != null) {
48+
tmp = tmp.children[c - 'a'];
49+
if (!tmp.isWord) {
50+
validWord = false;
51+
break;
52+
}
53+
}
54+
}
55+
if (validWord) {
56+
longestWord = word;
57+
}
58+
}
59+
return longestWord;
60+
}
61+
62+
private TrieNode buildTrie(String[] words) {
63+
TrieNode root = new TrieNode(' ');
64+
for (String word : words) {
65+
TrieNode tmp = root;
66+
for (char c : word.toCharArray()) {
67+
if (tmp.children[c - 'a'] == null) {
68+
tmp.children[c - 'a'] = new TrieNode(c);
69+
}
70+
tmp = tmp.children[c - 'a'];
71+
}
72+
tmp.isWord = true;
73+
}
74+
return root;
75+
}
76+
77+
class TrieNode {
78+
char val;
79+
boolean isWord;
80+
TrieNode[] children;
81+
82+
public TrieNode(char val) {
83+
this.val = val;
84+
this.isWord = false;
85+
this.children = new TrieNode[26];
86+
}
87+
}
88+
}
89+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._720;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _720Test {
10+
private static _720.Solution1 solution1;
11+
private static String[] words;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _720.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
words = new String[]{"w", "wo", "wor", "worl", "world"};
21+
assertEquals("world", solution1.longestWord(words));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
words = new String[]{"a", "banana", "app", "appl", "ap", "apply", "apple"};
27+
assertEquals("apple", solution1.longestWord(words));
28+
}
29+
30+
}

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