Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0536fee

Browse files
committed
720_Longest_Word_in_Dictionary
1 parent da6b87d commit 0536fee

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Python & JAVA Solutions for Leetcode (inspired by [haoel's leetcode](https://github.com/haoel/leetcode))
22

3-
Remember solutions are only solutions to given problems. If you want full study checklist for code & whiteboard interview, please turn to [jwasham's coding-interview-university](https://github.com/jwasham/coding-interview-university).
3+
Remember solutions are only solutions to given problems. If you want full study checklist for code & whiteboard interview, please turn to [jwasham's coding-interview-university](https://github.com/jwasham/coding-interview-university).
4+
5+
Also, there are open source implementations for basic data structs and algorithms, such as [Algorithms in Python](https://github.com/TheAlgorithms/Python) and [Algorithms in Java](https://github.com/TheAlgorithms/Java).
46

57
[Python](https://github.com/qiyuangong/leetcode/tree/master/python) and [Java](https://github.com/qiyuangong/leetcode/tree/master/java) full list. ♥ means you need a subscription.
68

@@ -149,6 +151,7 @@ Remember solutions are only solutions to given problems. If you want full study
149151
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/697_Degree_of_an_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/697_Degree_of_an_Array.java) | 1. Find degree and value, then find smallest subarray (start and end with this value), O(n) and O(n)<br>2. Go through nums, remember left most pos and right most for each value, O(n) and O(n) |
150152
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/709_To_Lower_Case.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/709_To_Lower_Case.java) | String processing:<br>1. str.lower() or str.toLowerCase()<br>2. ASCII processing. O(n) and O(1) |
151153
| 716 | [Max Stack](https://leetcode.com/problems/max-stack/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/716_Max_Stack.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/716_Max_Stack.java) | 1. Two stacks<br> 2. Double linked list and Hash |
154+
| 720 | [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/720_Longest_Word_in_Dictionary.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/720_Longest_Word_in_Dictionary.java) | 1. Brute Force, O(sum(w^2)) and O(w)<br>2. Tire Tree, O(sum(w) and O(w))<br>3. Sort and word without last char, O(nlogn + sum(w)) and O(w) |
152155
| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/743_Network_Delay_Time.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/743_Network_Delay_Time.java) | Let V == N, then: 1. DFS, O(V^V+ElgE), O(V+E)<br>2. Dijkstra, O(V^2+E), O(V+E)|
153156
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/771_Jewels_and_Stones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/771_Jewels_and_Stones.java) | Count given char in string. Hash or table. [Oneline](https://leetcode.com/problems/jewels-and-stones/discuss/113574/1-liners-PythonJavaRuby) |
154157
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/804_Unique_Morse_Code_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/804_Unique_Morse_Code_Words.java) | String, Hash and Set. Set is recommended. |
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
class Solution {
2+
/*public String longestWord(String[] words) {
3+
String ans = "";
4+
Set<String> wordset = new HashSet();
5+
for (String word: words) wordset.add(word);
6+
for (String word: words) {
7+
if (word.length() > ans.length() ||
8+
word.length() == ans.length() && word.compareTo(ans) < 0) {
9+
boolean good = true;
10+
for (int k = 1; k < word.length(); ++k) {
11+
if (!wordset.contains(word.substring(0, k))) {
12+
good = false;
13+
break;
14+
}
15+
}
16+
if (good) ans = word;
17+
}
18+
}
19+
return ans;
20+
}*/
21+
22+
public String longestWord(String[] words) {
23+
Trie trie = new Trie();
24+
int index = 0;
25+
for (String word: words) {
26+
trie.insert(word, ++index); //indexed by 1
27+
}
28+
trie.words = words;
29+
return trie.dfs();
30+
}
31+
}
32+
class Node {
33+
char c;
34+
HashMap<Character, Node> children = new HashMap();
35+
int end;
36+
public Node(char c){
37+
this.c = c;
38+
}
39+
}
40+
41+
class Trie {
42+
Node root;
43+
String[] words;
44+
public Trie() {
45+
root = new Node('0');
46+
}
47+
48+
public void insert(String word, int index) {
49+
Node cur = root;
50+
for (char c: word.toCharArray()) {
51+
cur.children.putIfAbsent(c, new Node(c));
52+
cur = cur.children.get(c);
53+
}
54+
cur.end = index;
55+
}
56+
57+
public String dfs() {
58+
String ans = "";
59+
Stack<Node> stack = new Stack();
60+
stack.push(root);
61+
while (!stack.empty()) {
62+
Node node = stack.pop();
63+
if (node.end > 0 || node == root) {
64+
if (node != root) {
65+
String word = words[node.end - 1];
66+
if (word.length() > ans.length() ||
67+
word.length() == ans.length() && word.compareTo(ans) < 0) {
68+
ans = word;
69+
}
70+
}
71+
for (Node nei: node.children.values()) {
72+
stack.push(nei);
73+
}
74+
}
75+
}
76+
return ans;
77+
}
78+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution(object):
2+
# def longestWord(self, words):
3+
# words.sort()
4+
# words_set, longest_word = set(['']), ''
5+
# for word in words:
6+
# if word[:-1] in words_set:
7+
# words_set.add(word)
8+
# if len(word) > len(longest_word):
9+
# longest_word = word
10+
# return longest_word
11+
12+
# def longestWord(self, words):
13+
# ans = ""
14+
# wordset = set(words)
15+
# for word in words:
16+
# if len(word) > len(ans) or len(word) == len(ans) and word < ans:
17+
# if all(word[:k] in wordset for k in xrange(1, len(word))):
18+
# ans = word
19+
# return ans
20+
21+
def longestWord(self, words):
22+
Trie = lambda: collections.defaultdict(Trie)
23+
trie = Trie()
24+
END = True
25+
for i, word in enumerate(words):
26+
reduce(dict.__getitem__, word, trie)[END] = i
27+
stack = trie.values()
28+
ans = ""
29+
while stack:
30+
cur = stack.pop()
31+
if END in cur:
32+
word = words[cur[END]]
33+
if len(word) > len(ans) or len(word) == len(ans) and word < ans:
34+
ans = word
35+
stack.extend([cur[letter] for letter in cur if letter != END])
36+
return ans

0 commit comments

Comments
 (0)