Skip to content

Commit b98f057

Browse files
committed
feat: add solutions to lc problem: No.1858
No.1858.Longest Word With All Prefixes
1 parent baaf2dc commit b98f057

File tree

6 files changed

+541
-5
lines changed

6 files changed

+541
-5
lines changed

Diff for: solution/1800-1899/1858.Longest Word With All Prefixes/README.md

+184
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,206 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:前缀树**
55+
5456
<!-- tabs:start -->
5557

5658
### **Python3**
5759

5860
<!-- 这里可写当前语言的特殊实现逻辑 -->
5961

6062
```python
63+
class Trie:
64+
def __init__(self):
65+
self.children = [None] * 26
66+
self.is_end = False
67+
68+
def insert(self, w):
69+
node = self
70+
for c in w:
71+
idx = ord(c) - ord("a")
72+
if node.children[idx] is None:
73+
node.children[idx] = Trie()
74+
node = node.children[idx]
75+
node.is_end = True
76+
77+
def search(self, w):
78+
node = self
79+
for c in w:
80+
idx = ord(c) - ord("a")
81+
node = node.children[idx]
82+
if not node.is_end:
83+
return False
84+
return True
85+
6186

87+
class Solution:
88+
def longestWord(self, words: List[str]) -> str:
89+
trie = Trie()
90+
for w in words:
91+
trie.insert(w)
92+
ans = ""
93+
for w in words:
94+
if ans and (len(ans) > len(w) or (len(ans) == len(w) and ans < w)):
95+
continue
96+
if trie.search(w):
97+
ans = w
98+
return ans
6299
```
63100

64101
### **Java**
65102

66103
<!-- 这里可写当前语言的特殊实现逻辑 -->
67104

68105
```java
106+
class Trie {
107+
Trie[] children = new Trie[26];
108+
boolean isEnd;
109+
110+
void insert(String w) {
111+
Trie node = this;
112+
for (char c : w.toCharArray()) {
113+
c -= 'a';
114+
if (node.children[c] == null) {
115+
node.children[c] = new Trie();
116+
}
117+
node = node.children[c];
118+
}
119+
node.isEnd = true;
120+
}
121+
122+
boolean search(String w) {
123+
Trie node = this;
124+
for (char c : w.toCharArray()) {
125+
c -= 'a';
126+
node = node.children[c];
127+
if (!node.isEnd) {
128+
return false;
129+
}
130+
}
131+
return true;
132+
}
133+
}
134+
135+
class Solution {
136+
public String longestWord(String[] words) {
137+
Trie trie = new Trie();
138+
for (String w : words) {
139+
trie.insert(w);
140+
}
141+
String ans = "";
142+
for (String w : words) {
143+
if (!"".equals(ans) && (ans.length() > w.length() || (ans.length() == w.length() && ans.compareTo(w) < 0))) {
144+
continue;
145+
}
146+
if (trie.search(w)) {
147+
ans = w;
148+
}
149+
}
150+
return ans;
151+
}
152+
}
153+
```
154+
155+
### **C++**
156+
157+
```cpp
158+
class Trie {
159+
private:
160+
vector<Trie*> children;
161+
bool isEnd;
162+
public:
163+
Trie() : children(26), isEnd(false) {}
164+
165+
void insert(string word) {
166+
Trie* node = this;
167+
for (char c : word)
168+
{
169+
c -= 'a';
170+
if (!node->children[c]) node->children[c] = new Trie();
171+
node = node->children[c];
172+
}
173+
node->isEnd = true;
174+
}
175+
176+
bool search(string word) {
177+
Trie* node = this;
178+
for (char c : word)
179+
{
180+
c -= 'a';
181+
node = node->children[c];
182+
if (!node->isEnd) return false;
183+
}
184+
return true;
185+
}
186+
};
187+
188+
class Solution {
189+
public:
190+
string longestWord(vector<string>& words) {
191+
Trie* trie = new Trie();
192+
for (auto w : words) trie->insert(w);
193+
string ans = "";
194+
for (auto w : words)
195+
{
196+
if (ans != "" && (ans.size() > w.size() || (ans.size() == w.size() && ans < w))) continue;
197+
if (trie->search(w)) ans = w;
198+
}
199+
return ans;
200+
}
201+
};
202+
```
203+
204+
### **Go**
205+
206+
```go
207+
type Trie struct {
208+
children [26]*Trie
209+
isEnd bool
210+
}
211+
212+
func newTrie() *Trie {
213+
return &Trie{}
214+
}
215+
func (this *Trie) insert(word string) {
216+
node := this
217+
for _, c := range word {
218+
c -= 'a'
219+
if node.children[c] == nil {
220+
node.children[c] = newTrie()
221+
}
222+
node = node.children[c]
223+
}
224+
node.isEnd = true
225+
}
226+
func (this *Trie) search(word string) bool {
227+
node := this
228+
for _, c := range word {
229+
c -= 'a'
230+
node = node.children[c]
231+
if !node.isEnd {
232+
return false
233+
}
234+
}
235+
return true
236+
}
69237
238+
func longestWord(words []string) string {
239+
trie := newTrie()
240+
for _, w := range words {
241+
trie.insert(w)
242+
}
243+
ans := ""
244+
for _, w := range words {
245+
if ans != "" && (len(ans) > len(w) || (len(ans) == len(w) && ans < w)) {
246+
continue
247+
}
248+
if trie.search(w) {
249+
ans = w
250+
}
251+
}
252+
return ans
253+
}
70254
```
71255

72256
### **...**

0 commit comments

Comments
 (0)