56
56
57
57
<!-- solution:start -->
58
58
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)$。
60
66
61
67
<!-- tabs:start -->
62
68
@@ -65,19 +71,19 @@ tags:
65
71
``` python
66
72
class Solution :
67
73
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 )
70
76
i = j = 0
71
77
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
81
87
return ans
82
88
```
83
89
@@ -87,26 +93,24 @@ class Solution:
87
93
class Solution {
88
94
public String findLongestWord (String s , List<String > dictionary ) {
89
95
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;
95
100
}
96
101
}
97
102
return ans;
98
103
}
99
104
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 ;
106
111
}
107
- ++ i;
108
112
}
109
- return j == n ;
113
+ return i == m ;
110
114
}
111
115
}
112
116
```
@@ -118,20 +122,23 @@ class Solution {
118
122
public:
119
123
string findLongestWord(string s, vector<string >& dictionary) {
120
124
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
+ }
133
140
}
134
- return j == n ;
141
+ return ans ;
135
142
}
136
143
};
137
144
```
@@ -140,21 +147,21 @@ public:
140
147
141
148
```go
142
149
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 ++
150
157
}
151
- i++
152
158
}
153
- return j == n
159
+ return i == m
154
160
}
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
158
165
}
159
166
}
160
167
return ans
@@ -165,57 +172,57 @@ func findLongestWord(s string, dictionary []string) string {
165
172
166
173
``` ts
167
174
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 ];
180
177
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 ;
185
181
}
186
- i ++ ;
187
182
}
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 ;
190
190
}
191
191
}
192
- return ' ' ;
192
+ return ans ;
193
193
}
194
194
```
195
195
196
196
#### Rust
197
197
198
198
``` rust
199
199
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 ;
213
207
}
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 ;
216
222
}
223
+ j += 1 ;
217
224
}
218
- String :: new ()
225
+ i == m
219
226
}
220
227
}
221
228
```
0 commit comments