Skip to content

Commit cfbdc53

Browse files
authored
feat: add solutions to lc problems: No.524,525 (#3116)
* No.0524.Longest Word in Dictionary through Deleting * No.0525.Contiguous Array
1 parent 06616ea commit cfbdc53

File tree

16 files changed

+452
-363
lines changed

16 files changed

+452
-363
lines changed

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

+93-86
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### 方法一
59+
### 方法一:判断子序列
60+
61+
我们定义一个函数 $check(s, t)$,用于判断字符串 $s$ 是否是字符串 $t$ 的子序列。我们可以使用双指针的方法,初始化两个指针 $i$ 和 $j$ 分别指向字符串 $s$ 和字符串 $t$ 的开头,然后不断移动指针 $j$,如果 $s[i]$ 和 $t[j]$ 相等,则移动指针 $i$,最后判断 $i$ 是否等于 $s$ 的长度即可。若 $i$ 等于 $s$ 的长度,则说明 $s$ 是 $t$ 的子序列。
62+
63+
我们初始化答案字符串 $ans$ 为空字符串,然后遍历数组 $dictionary$ 中的每个字符串 $t$,如果 $t$ 是 $s$ 的子序列,并且 $t$ 的长度大于 $ans$ 的长度,或者 $t$ 的长度等于 $ans$ 的长度且 $t$ 字典序小于 $ans$,则更新 $ans$ 为 $t$。
64+
65+
时间复杂度 $O(d \times (m + n))$,其中 $d$ 是字符串列表的长度,而 $m$ 和 $n$ 分别是字符串 $s$ 的长度和字符串列表中字符串的平均长度。空间复杂度 $O(1)$。
6066

6167
<!-- tabs:start -->
6268

@@ -65,19 +71,19 @@ tags:
6571
```python
6672
class Solution:
6773
def findLongestWord(self, s: str, dictionary: List[str]) -> str:
68-
def check(a, b):
69-
m, n = len(a), len(b)
74+
def check(s: str, t: str) -> bool:
75+
m, n = len(s), len(t)
7076
i = j = 0
7177
while i < m and j < n:
72-
if a[i] == b[j]:
73-
j += 1
74-
i += 1
75-
return j == n
76-
77-
ans = ''
78-
for a in dictionary:
79-
if check(s, a) and (len(ans) < len(a) or (len(ans) == len(a) and ans > a)):
80-
ans = a
78+
if s[i] == t[j]:
79+
i += 1
80+
j += 1
81+
return i == m
82+
83+
ans = ""
84+
for t in dictionary:
85+
if check(t, s) and (len(ans) < len(t) or (len(ans) == len(t) and ans > t)):
86+
ans = t
8187
return ans
8288
```
8389

@@ -87,26 +93,24 @@ class Solution:
8793
class Solution {
8894
public String findLongestWord(String s, List<String> dictionary) {
8995
String ans = "";
90-
for (String a : dictionary) {
91-
if (check(s, a)
92-
&& (ans.length() < a.length()
93-
|| (ans.length() == a.length() && a.compareTo(ans) < 0))) {
94-
ans = a;
96+
for (String t : dictionary) {
97+
int a = ans.length(), b = t.length();
98+
if (check(t, s) && (a < b || (a == b && t.compareTo(ans) < 0))) {
99+
ans = t;
95100
}
96101
}
97102
return ans;
98103
}
99104

100-
private boolean check(String a, String b) {
101-
int m = a.length(), n = b.length();
102-
int i = 0, j = 0;
103-
while (i < m && j < n) {
104-
if (a.charAt(i) == b.charAt(j)) {
105-
++j;
105+
private boolean check(String s, String t) {
106+
int m = s.length(), n = t.length();
107+
int i = 0;
108+
for (int j = 0; i < m && j < n; ++j) {
109+
if (s.charAt(i) == t.charAt(j)) {
110+
++i;
106111
}
107-
++i;
108112
}
109-
return j == n;
113+
return i == m;
110114
}
111115
}
112116
```
@@ -118,20 +122,23 @@ class Solution {
118122
public:
119123
string findLongestWord(string s, vector<string>& dictionary) {
120124
string ans = "";
121-
for (string& a : dictionary)
122-
if (check(s, a) && (ans.size() < a.size() || (ans.size() == a.size() && a < ans)))
123-
ans = a;
124-
return ans;
125-
}
126-
127-
bool check(string& a, string& b) {
128-
int m = a.size(), n = b.size();
129-
int i = 0, j = 0;
130-
while (i < m && j < n) {
131-
if (a[i] == b[j]) ++j;
132-
++i;
125+
auto check = [&](const string& s, const string& t) {
126+
int m = s.size(), n = t.size();
127+
int i = 0;
128+
for (int j = 0; i < m && j < n; ++j) {
129+
if (s[i] == t[j]) {
130+
++i;
131+
}
132+
}
133+
return i == m;
134+
};
135+
for (auto& t : dictionary) {
136+
int a = ans.size(), b = t.size();
137+
if (check(t, s) && (a < b || (a == b && ans > t))) {
138+
ans = t;
139+
}
133140
}
134-
return j == n;
141+
return ans;
135142
}
136143
};
137144
```
@@ -140,21 +147,21 @@ public:
140147
141148
```go
142149
func findLongestWord(s string, dictionary []string) string {
143-
ans := ""
144-
check := func(a, b string) bool {
145-
m, n := len(a), len(b)
146-
i, j := 0, 0
147-
for i < m && j < n {
148-
if a[i] == b[j] {
149-
j++
150+
ans := ''
151+
check := func(s, t string) bool {
152+
m, n := len(s), len(t)
153+
i := 0
154+
for j := 0; i < m && j < n; j++ {
155+
if s[i] == t[j] {
156+
i++
150157
}
151-
i++
152158
}
153-
return j == n
159+
return i == m
154160
}
155-
for _, a := range dictionary {
156-
if check(s, a) && (len(ans) < len(a) || (len(ans) == len(a) && a < ans)) {
157-
ans = a
161+
for _, t := range dictionary {
162+
a, b := len(ans), len(t)
163+
if check(t, s) && (a < b || (a == b && ans > t)) {
164+
ans = t
158165
}
159166
}
160167
return ans
@@ -165,57 +172,57 @@ func findLongestWord(s string, dictionary []string) string {
165172

166173
```ts
167174
function findLongestWord(s: string, dictionary: string[]): string {
168-
dictionary.sort((a, b) => {
169-
if (a.length === b.length) {
170-
return b < a ? 1 : -1;
171-
}
172-
return b.length - a.length;
173-
});
174-
const n = s.length;
175-
for (const target of dictionary) {
176-
const m = target.length;
177-
if (m > n) {
178-
continue;
179-
}
175+
const check = (s: string, t: string): boolean => {
176+
const [m, n] = [s.length, t.length];
180177
let i = 0;
181-
let j = 0;
182-
while (i < n && j < m) {
183-
if (s[i] === target[j]) {
184-
j++;
178+
for (let j = 0; i < m && j < n; ++j) {
179+
if (s[i] === t[j]) {
180+
++i;
185181
}
186-
i++;
187182
}
188-
if (j === m) {
189-
return target;
183+
return i === m;
184+
};
185+
let ans: string = '';
186+
for (const t of dictionary) {
187+
const [a, b] = [ans.length, t.length];
188+
if (check(t, s) && (a < b || (a === b && ans > t))) {
189+
ans = t;
190190
}
191191
}
192-
return '';
192+
return ans;
193193
}
194194
```
195195

196196
#### Rust
197197

198198
```rust
199199
impl Solution {
200-
pub fn find_longest_word(s: String, mut dictionary: Vec<String>) -> String {
201-
dictionary.sort_unstable_by(|a, b| (b.len(), a).cmp(&(a.len(), b)));
202-
for target in dictionary {
203-
let target: Vec<char> = target.chars().collect();
204-
let n = target.len();
205-
let mut i = 0;
206-
for c in s.chars() {
207-
if i == n {
208-
break;
209-
}
210-
if c == target[i] {
211-
i += 1;
212-
}
200+
pub fn find_longest_word(s: String, dictionary: Vec<String>) -> String {
201+
let mut ans = String::new();
202+
for t in dictionary {
203+
let a = ans.len();
204+
let b = t.len();
205+
if Self::check(&t, &s) && (a < b || (a == b && t < ans)) {
206+
ans = t;
213207
}
214-
if i == n {
215-
return target.iter().collect();
208+
}
209+
ans
210+
}
211+
212+
fn check(s: &str, t: &str) -> bool {
213+
let (m, n) = (s.len(), t.len());
214+
let mut i = 0;
215+
let mut j = 0;
216+
let s: Vec<char> = s.chars().collect();
217+
let t: Vec<char> = t.chars().collect();
218+
219+
while i < m && j < n {
220+
if s[i] == t[j] {
221+
i += 1;
216222
}
223+
j += 1;
217224
}
218-
String::new()
225+
i == m
219226
}
220227
}
221228
```

0 commit comments

Comments
 (0)