Skip to content

Commit de35ad4

Browse files
authored
feat: add solutions to lc problems
* No.0524.Longest Word in Dictionary through Deleting * No.0530.Minimum Absolute Difference in BST * No.0783.Minimum Distance Between BST Nodes
1 parent 34f97b5 commit de35ad4

File tree

18 files changed

+858
-248
lines changed

18 files changed

+858
-248
lines changed

solution/0500-0599/0524.Longest Word in Dictionary through Deleting/README.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,52 @@
4848
<!-- 这里可写当前语言的特殊实现逻辑 -->
4949

5050
```python
51-
51+
class Solution:
52+
def findLongestWord(self, s: str, dictionary: List[str]) -> str:
53+
def check(a, b):
54+
m, n = len(a), len(b)
55+
i = j = 0
56+
while i < m and j < n:
57+
if a[i] == b[j]:
58+
j += 1
59+
i += 1
60+
return j == n
61+
62+
ans = ''
63+
for a in dictionary:
64+
if check(s, a) and (len(ans) < len(a) or (len(ans) == len(a) and ans > a)):
65+
ans = a
66+
return ans
5267
```
5368

5469
### **Java**
5570

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

5873
```java
74+
class Solution {
75+
public String findLongestWord(String s, List<String> dictionary) {
76+
String ans = "";
77+
for (String a : dictionary) {
78+
if (check(s, a) && (ans.length() < a.length() || (ans.length() == a.length() && a.compareTo(ans) < 0))) {
79+
ans = a;
80+
}
81+
}
82+
return ans;
83+
}
5984

85+
private boolean check(String a, String b) {
86+
int m = a.length(), n = b.length();
87+
int i = 0, j = 0;
88+
while (i < m && j < n) {
89+
if (a.charAt(i) == b.charAt(j)) {
90+
++j;
91+
}
92+
++i;
93+
}
94+
return j == n;
95+
}
96+
}
6097
```
6198

6299
### **TypeScript**
@@ -118,6 +155,57 @@ impl Solution {
118155
}
119156
```
120157

158+
### **C++**
159+
160+
```cpp
161+
class Solution {
162+
public:
163+
string findLongestWord(string s, vector<string>& dictionary) {
164+
string ans = "";
165+
for (string& a : dictionary)
166+
if (check(s, a) && (ans.size() < a.size() || (ans.size() == a.size() && a < ans)))
167+
ans = a;
168+
return ans;
169+
}
170+
171+
bool check(string& a, string& b) {
172+
int m = a.size(), n = b.size();
173+
int i = 0, j = 0;
174+
while (i < m && j < n)
175+
{
176+
if (a[i] == b[j]) ++j;
177+
++i;
178+
}
179+
return j == n;
180+
}
181+
};
182+
```
183+
184+
### **Go**
185+
186+
```go
187+
func findLongestWord(s string, dictionary []string) string {
188+
ans := ""
189+
check := func(a, b string) bool {
190+
m, n := len(a), len(b)
191+
i, j := 0, 0
192+
for i < m && j < n {
193+
if a[i] == b[j] {
194+
j++
195+
}
196+
i++
197+
}
198+
return j == n
199+
}
200+
for _, a := range dictionary {
201+
if check(s, a) && (len(ans) < len(a) || (len(ans) == len(a) && a < ans)) {
202+
ans = a
203+
}
204+
}
205+
return ans
206+
}
207+
```
208+
121209
### **...**
122210

123211
```

solution/0500-0599/0524.Longest Word in Dictionary through Deleting/README_EN.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,50 @@
3838
### **Python3**
3939

4040
```python
41-
41+
class Solution:
42+
def findLongestWord(self, s: str, dictionary: List[str]) -> str:
43+
def check(a, b):
44+
m, n = len(a), len(b)
45+
i = j = 0
46+
while i < m and j < n:
47+
if a[i] == b[j]:
48+
j += 1
49+
i += 1
50+
return j == n
51+
52+
ans = ''
53+
for a in dictionary:
54+
if check(s, a) and (len(ans) < len(a) or (len(ans) == len(a) and ans > a)):
55+
ans = a
56+
return ans
4257
```
4358

4459
### **Java**
4560

4661
```java
62+
class Solution {
63+
public String findLongestWord(String s, List<String> dictionary) {
64+
String ans = "";
65+
for (String a : dictionary) {
66+
if (check(s, a) && (ans.length() < a.length() || (ans.length() == a.length() && a.compareTo(ans) < 0))) {
67+
ans = a;
68+
}
69+
}
70+
return ans;
71+
}
4772

73+
private boolean check(String a, String b) {
74+
int m = a.length(), n = b.length();
75+
int i = 0, j = 0;
76+
while (i < m && j < n) {
77+
if (a.charAt(i) == b.charAt(j)) {
78+
++j;
79+
}
80+
++i;
81+
}
82+
return j == n;
83+
}
84+
}
4885
```
4986

5087
### **TypeScript**
@@ -106,6 +143,57 @@ impl Solution {
106143
}
107144
```
108145

146+
### **C++**
147+
148+
```cpp
149+
class Solution {
150+
public:
151+
string findLongestWord(string s, vector<string>& dictionary) {
152+
string ans = "";
153+
for (string& a : dictionary)
154+
if (check(s, a) && (ans.size() < a.size() || (ans.size() == a.size() && a < ans)))
155+
ans = a;
156+
return ans;
157+
}
158+
159+
bool check(string& a, string& b) {
160+
int m = a.size(), n = b.size();
161+
int i = 0, j = 0;
162+
while (i < m && j < n)
163+
{
164+
if (a[i] == b[j]) ++j;
165+
++i;
166+
}
167+
return j == n;
168+
}
169+
};
170+
```
171+
172+
### **Go**
173+
174+
```go
175+
func findLongestWord(s string, dictionary []string) string {
176+
ans := ""
177+
check := func(a, b string) bool {
178+
m, n := len(a), len(b)
179+
i, j := 0, 0
180+
for i < m && j < n {
181+
if a[i] == b[j] {
182+
j++
183+
}
184+
i++
185+
}
186+
return j == n
187+
}
188+
for _, a := range dictionary {
189+
if check(s, a) && (len(ans) < len(a) || (len(ans) == len(a) && a < ans)) {
190+
ans = a
191+
}
192+
}
193+
return ans
194+
}
195+
```
196+
109197
### **...**
110198

111199
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
string findLongestWord(string s, vector<string>& dictionary) {
4+
string ans = "";
5+
for (string& a : dictionary)
6+
if (check(s, a) && (ans.size() < a.size() || (ans.size() == a.size() && a < ans)))
7+
ans = a;
8+
return ans;
9+
}
10+
11+
bool check(string& a, string& b) {
12+
int m = a.size(), n = b.size();
13+
int i = 0, j = 0;
14+
while (i < m && j < n)
15+
{
16+
if (a[i] == b[j]) ++j;
17+
++i;
18+
}
19+
return j == n;
20+
}
21+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func findLongestWord(s string, dictionary []string) string {
2+
ans := ""
3+
check := func(a, b string) bool {
4+
m, n := len(a), len(b)
5+
i, j := 0, 0
6+
for i < m && j < n {
7+
if a[i] == b[j] {
8+
j++
9+
}
10+
i++
11+
}
12+
return j == n
13+
}
14+
for _, a := range dictionary {
15+
if check(s, a) && (len(ans) < len(a) || (len(ans) == len(a) && a < ans)) {
16+
ans = a
17+
}
18+
}
19+
return ans
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public String findLongestWord(String s, List<String> dictionary) {
3+
String ans = "";
4+
for (String a : dictionary) {
5+
if (check(s, a) && (ans.length() < a.length() || (ans.length() == a.length() && a.compareTo(ans) < 0))) {
6+
ans = a;
7+
}
8+
}
9+
return ans;
10+
}
11+
12+
private boolean check(String a, String b) {
13+
int m = a.length(), n = b.length();
14+
int i = 0, j = 0;
15+
while (i < m && j < n) {
16+
if (a.charAt(i) == b.charAt(j)) {
17+
++j;
18+
}
19+
++i;
20+
}
21+
return j == n;
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def findLongestWord(self, s: str, dictionary: List[str]) -> str:
3+
def check(a, b):
4+
m, n = len(a), len(b)
5+
i = j = 0
6+
while i < m and j < n:
7+
if a[i] == b[j]:
8+
j += 1
9+
i += 1
10+
return j == n
11+
12+
ans = ''
13+
for a in dictionary:
14+
if check(s, a) and (len(ans) < len(a) or (len(ans) == len(a) and ans > a)):
15+
ans = a
16+
return ans

0 commit comments

Comments
 (0)