Skip to content

Commit 5e0b388

Browse files
committed
feat: add solutions to lc problem: No.0720
No.0720.Longest Word in Dictionary
1 parent 24773bd commit 5e0b388

File tree

6 files changed

+318
-2
lines changed

6 files changed

+318
-2
lines changed

solution/0700-0799/0720.Longest Word in Dictionary/README.md

+113-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
**方法一:哈希表**
46+
47+
用哈希表存放所有单词。遍历这些单词,找出**长度最长且字典序最小**的单词。
48+
49+
**方法二:排序**
50+
4551
优先返回符合条件、**长度最长且字典序最小**的单词,那么可以进行依照该规则,先对 `words` 进行排序,免去多个结果之间的比较。
4652

4753
<!-- tabs:start -->
@@ -51,15 +57,55 @@
5157
<!-- 这里可写当前语言的特殊实现逻辑 -->
5258

5359
```python
54-
60+
class Solution:
61+
def longestWord(self, words: List[str]) -> str:
62+
cnt, ans = 0, ''
63+
s = set(words)
64+
for w in s:
65+
n = len(w)
66+
if all(w[:i] in s for i in range(1, n)):
67+
if cnt < n:
68+
cnt, ans = n, w
69+
elif cnt == n and w < ans:
70+
ans = w
71+
return ans
5572
```
5673

5774
### **Java**
5875

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

6178
```java
79+
class Solution {
80+
private Set<String> s;
81+
82+
public String longestWord(String[] words) {
83+
s = new HashSet<>(Arrays.asList(words));
84+
int cnt = 0;
85+
String ans = "";
86+
for (String w : s) {
87+
int n = w.length();
88+
if (check(w)) {
89+
if (cnt < n) {
90+
cnt = n;
91+
ans = w;
92+
} else if (cnt == n && w.compareTo(ans) < 0) {
93+
ans = w;
94+
}
95+
}
96+
}
97+
return ans;
98+
}
6299

100+
private boolean check(String word) {
101+
for (int i = 1, n = word.length(); i < n; ++i) {
102+
if (!s.contains(word.substring(0, i))) {
103+
return false;
104+
}
105+
}
106+
return true;
107+
}
108+
}
63109
```
64110

65111
### **TypeScript**
@@ -113,6 +159,72 @@ impl Solution {
113159
}
114160
```
115161

162+
### **C++**
163+
164+
```cpp
165+
class Solution {
166+
public:
167+
string longestWord(vector<string>& words) {
168+
unordered_set<string> s(words.begin(), words.end());
169+
int cnt = 0;
170+
string ans = "";
171+
for (auto w : s)
172+
{
173+
int n = w.size();
174+
if (check(w, s))
175+
{
176+
if (cnt < n)
177+
{
178+
cnt = n;
179+
ans = w;
180+
}
181+
else if (cnt == n && w < ans) ans = w;
182+
}
183+
}
184+
return ans;
185+
}
186+
187+
bool check(string& word, unordered_set<string>& s) {
188+
for (int i = 1, n = word.size(); i < n; ++i)
189+
if (!s.count(word.substr(0, i)))
190+
return false;
191+
return true;
192+
}
193+
};
194+
```
195+
196+
### **Go**
197+
198+
```go
199+
func longestWord(words []string) string {
200+
s := make(map[string]bool)
201+
for _, w := range words {
202+
s[w] = true
203+
}
204+
cnt := 0
205+
ans := ""
206+
check := func(word string) bool {
207+
for i, n := 1, len(word); i < n; i++ {
208+
if !s[word[:i]] {
209+
return false
210+
}
211+
}
212+
return true
213+
}
214+
for w, _ := range s {
215+
n := len(w)
216+
if check(w) {
217+
if cnt < n {
218+
cnt, ans = n, w
219+
} else if cnt == n && w < ans {
220+
ans = w
221+
}
222+
}
223+
}
224+
return ans
225+
}
226+
```
227+
116228
### **...**
117229

118230
```

solution/0700-0799/0720.Longest Word in Dictionary/README_EN.md

+107-1
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,53 @@
4141
### **Python3**
4242

4343
```python
44-
44+
class Solution:
45+
def longestWord(self, words: List[str]) -> str:
46+
cnt, ans = 0, ''
47+
s = set(words)
48+
for w in s:
49+
n = len(w)
50+
if all(w[:i] in s for i in range(1, n)):
51+
if cnt < n:
52+
cnt, ans = n, w
53+
elif cnt == n and w < ans:
54+
ans = w
55+
return ans
4556
```
4657

4758
### **Java**
4859

4960
```java
61+
class Solution {
62+
private Set<String> s;
63+
64+
public String longestWord(String[] words) {
65+
s = new HashSet<>(Arrays.asList(words));
66+
int cnt = 0;
67+
String ans = "";
68+
for (String w : s) {
69+
int n = w.length();
70+
if (check(w)) {
71+
if (cnt < n) {
72+
cnt = n;
73+
ans = w;
74+
} else if (cnt == n && w.compareTo(ans) < 0) {
75+
ans = w;
76+
}
77+
}
78+
}
79+
return ans;
80+
}
5081

82+
private boolean check(String word) {
83+
for (int i = 1, n = word.length(); i < n; ++i) {
84+
if (!s.contains(word.substring(0, i))) {
85+
return false;
86+
}
87+
}
88+
return true;
89+
}
90+
}
5191
```
5292

5393
### **TypeScript**
@@ -101,6 +141,72 @@ impl Solution {
101141
}
102142
```
103143

144+
### **C++**
145+
146+
```cpp
147+
class Solution {
148+
public:
149+
string longestWord(vector<string>& words) {
150+
unordered_set<string> s(words.begin(), words.end());
151+
int cnt = 0;
152+
string ans = "";
153+
for (auto w : s)
154+
{
155+
int n = w.size();
156+
if (check(w, s))
157+
{
158+
if (cnt < n)
159+
{
160+
cnt = n;
161+
ans = w;
162+
}
163+
else if (cnt == n && w < ans) ans = w;
164+
}
165+
}
166+
return ans;
167+
}
168+
169+
bool check(string& word, unordered_set<string>& s) {
170+
for (int i = 1, n = word.size(); i < n; ++i)
171+
if (!s.count(word.substr(0, i)))
172+
return false;
173+
return true;
174+
}
175+
};
176+
```
177+
178+
### **Go**
179+
180+
```go
181+
func longestWord(words []string) string {
182+
s := make(map[string]bool)
183+
for _, w := range words {
184+
s[w] = true
185+
}
186+
cnt := 0
187+
ans := ""
188+
check := func(word string) bool {
189+
for i, n := 1, len(word); i < n; i++ {
190+
if !s[word[:i]] {
191+
return false
192+
}
193+
}
194+
return true
195+
}
196+
for w, _ := range s {
197+
n := len(w)
198+
if check(w) {
199+
if cnt < n {
200+
cnt, ans = n, w
201+
} else if cnt == n && w < ans {
202+
ans = w
203+
}
204+
}
205+
}
206+
return ans
207+
}
208+
```
209+
104210
### **...**
105211

106212
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
string longestWord(vector<string>& words) {
4+
unordered_set<string> s(words.begin(), words.end());
5+
int cnt = 0;
6+
string ans = "";
7+
for (auto w : s)
8+
{
9+
int n = w.size();
10+
if (check(w, s))
11+
{
12+
if (cnt < n)
13+
{
14+
cnt = n;
15+
ans = w;
16+
}
17+
else if (cnt == n && w < ans) ans = w;
18+
}
19+
}
20+
return ans;
21+
}
22+
23+
bool check(string& word, unordered_set<string>& s) {
24+
for (int i = 1, n = word.size(); i < n; ++i)
25+
if (!s.count(word.substr(0, i)))
26+
return false;
27+
return true;
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
func longestWord(words []string) string {
2+
s := make(map[string]bool)
3+
for _, w := range words {
4+
s[w] = true
5+
}
6+
cnt := 0
7+
ans := ""
8+
check := func(word string) bool {
9+
for i, n := 1, len(word); i < n; i++ {
10+
if !s[word[:i]] {
11+
return false
12+
}
13+
}
14+
return true
15+
}
16+
for w, _ := range s {
17+
n := len(w)
18+
if check(w) {
19+
if cnt < n {
20+
cnt, ans = n, w
21+
} else if cnt == n && w < ans {
22+
ans = w
23+
}
24+
}
25+
}
26+
return ans
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
private Set<String> s;
3+
4+
public String longestWord(String[] words) {
5+
s = new HashSet<>(Arrays.asList(words));
6+
int cnt = 0;
7+
String ans = "";
8+
for (String w : s) {
9+
int n = w.length();
10+
if (check(w)) {
11+
if (cnt < n) {
12+
cnt = n;
13+
ans = w;
14+
} else if (cnt == n && w.compareTo(ans) < 0) {
15+
ans = w;
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
22+
private boolean check(String word) {
23+
for (int i = 1, n = word.length(); i < n; ++i) {
24+
if (!s.contains(word.substring(0, i))) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def longestWord(self, words: List[str]) -> str:
3+
cnt, ans = 0, ''
4+
s = set(words)
5+
for w in s:
6+
n = len(w)
7+
if all(w[:i] in s for i in range(1, n)):
8+
if cnt < n:
9+
cnt, ans = n, w
10+
elif cnt == n and w < ans:
11+
ans = w
12+
return ans

0 commit comments

Comments
 (0)