@@ -80,32 +80,250 @@ tags:
80
80
81
81
<!-- solution:start -->
82
82
83
- ### 方法一
83
+ ### 方法一:BFS
84
+
85
+ 我们可以先统计字符串中每个字符出现的次数,然后将出现次数大于等于 $k$ 的字符按从小到大的顺序存入一个列表 $\textit{cs}$ 中。接下来,我们可以使用 BFS 来枚举所有可能的子序列。
86
+
87
+ 我们定义一个队列 $\textit{q}$,初始时将空字符串放入队列中。然后,我们从队列中取出一个字符串 $\textit{cur}$,并尝试将每个字符 $c \in \textit{cs}$ 添加到 $\textit{cur}$ 的末尾,形成一个新的字符串 $\textit{nxt}$。如果 $\textit{nxt}$ 是一个重复 $k$ 次的子序列,我们就将其加入到答案中,并将 $\textit{nxt}$ 放入队列中继续处理。
88
+
89
+ 我们需要一个辅助函数 $\textit{check(t, k)}$ 来判断字符串 $\textit{t}$ 是否是字符串 $s$ 的一个重复 $k$ 次的子序列。具体地,我们可以使用两个指针来遍历字符串 $s$ 和 $\textit{t}$,如果在遍历过程中能够找到 $\textit{t}$ 的所有字符,并且能够重复 $k$ 次,那么就返回 $\textit{true}$,否则返回 $\textit{false}$。
84
90
85
91
<!-- tabs:start -->
86
92
87
93
#### Python3
88
94
89
95
``` python
90
-
96
+ class Solution :
97
+ def longestSubsequenceRepeatedK (self , s : str , k : int ) -> str :
98
+ def check (t : str , k : int ) -> bool :
99
+ i = 0
100
+ for c in s:
101
+ if c == t[i]:
102
+ i += 1
103
+ if i == len (t):
104
+ k -= 1
105
+ if k == 0 :
106
+ return True
107
+ i = 0
108
+ return False
109
+
110
+ cnt = Counter(s)
111
+ cs = [c for c in ascii_lowercase if cnt[c] >= k]
112
+ q = deque([" " ])
113
+ ans = " "
114
+ while q:
115
+ cur = q.popleft()
116
+ for c in cs:
117
+ nxt = cur + c
118
+ if check(nxt, k):
119
+ ans = nxt
120
+ q.append(nxt)
121
+ return ans
91
122
```
92
123
93
124
#### Java
94
125
95
126
``` java
96
-
127
+ class Solution {
128
+ private char [] s;
129
+
130
+ public String longestSubsequenceRepeatedK (String s , int k ) {
131
+ this . s = s. toCharArray();
132
+ int [] cnt = new int [26 ];
133
+ for (char c : this . s) {
134
+ cnt[c - ' a' ]++ ;
135
+ }
136
+
137
+ List<Character > cs = new ArrayList<> ();
138
+ for (char c = ' a' ; c <= ' z' ; ++ c) {
139
+ if (cnt[c - ' a' ] >= k) {
140
+ cs. add(c);
141
+ }
142
+ }
143
+ Deque<String > q = new ArrayDeque<> ();
144
+ q. offer(" " );
145
+ String ans = " " ;
146
+ while (! q. isEmpty()) {
147
+ String cur = q. poll();
148
+ for (char c : cs) {
149
+ String nxt = cur + c;
150
+ if (check(nxt, k)) {
151
+ ans = nxt;
152
+ q. offer(nxt);
153
+ }
154
+ }
155
+ }
156
+ return ans;
157
+ }
158
+
159
+ private boolean check (String t , int k ) {
160
+ int i = 0 ;
161
+ for (char c : s) {
162
+ if (c == t. charAt(i)) {
163
+ i++ ;
164
+ if (i == t. length()) {
165
+ if (-- k == 0 ) {
166
+ return true ;
167
+ }
168
+ i = 0 ;
169
+ }
170
+ }
171
+ }
172
+ return false ;
173
+ }
174
+ }
97
175
```
98
176
99
177
#### C++
100
178
101
179
``` cpp
102
-
180
+ class Solution {
181
+ public:
182
+ string longestSubsequenceRepeatedK(string s, int k) {
183
+ auto check = [ &] (const string& t, int k) -> bool {
184
+ int i = 0;
185
+ for (char c : s) {
186
+ if (c == t[ i] ) {
187
+ i++;
188
+ if (i == t.size()) {
189
+ if (--k == 0) {
190
+ return true;
191
+ }
192
+ i = 0;
193
+ }
194
+ }
195
+ }
196
+ return false;
197
+ };
198
+ int cnt[ 26] = {};
199
+ for (char c : s) {
200
+ cnt[ c - 'a'] ++;
201
+ }
202
+
203
+ vector<char> cs;
204
+ for (char c = 'a'; c <= 'z'; ++c) {
205
+ if (cnt[c - 'a'] >= k) {
206
+ cs.push_back(c);
207
+ }
208
+ }
209
+
210
+ queue<string> q;
211
+ q.push(" " );
212
+ string ans;
213
+ while (!q.empty()) {
214
+ string cur = q.front();
215
+ q.pop();
216
+ for (char c : cs) {
217
+ string nxt = cur + c;
218
+ if (check(nxt, k)) {
219
+ ans = nxt;
220
+ q.push(nxt);
221
+ }
222
+ }
223
+ }
224
+ return ans;
225
+ }
226
+ };
103
227
```
104
228
105
229
#### Go
106
230
107
231
``` go
232
+ func longestSubsequenceRepeatedK (s string , k int ) string {
233
+ check := func (t string , k int ) bool {
234
+ i := 0
235
+ for _ , c := range s {
236
+ if byte (c) == t[i] {
237
+ i++
238
+ if i == len (t) {
239
+ k--
240
+ if k == 0 {
241
+ return true
242
+ }
243
+ i = 0
244
+ }
245
+ }
246
+ }
247
+ return false
248
+ }
249
+
250
+ cnt := [26 ]int {}
251
+ for i := 0 ; i < len (s); i++ {
252
+ cnt[s[i]-' a' ]++
253
+ }
254
+
255
+ cs := []byte {}
256
+ for c := byte (' a' ); c <= ' z' ; c++ {
257
+ if cnt[c-' a' ] >= k {
258
+ cs = append (cs, c)
259
+ }
260
+ }
261
+
262
+ q := []string {" " }
263
+ ans := " "
264
+ for len (q) > 0 {
265
+ cur := q[0 ]
266
+ q = q[1 :]
267
+ for _ , c := range cs {
268
+ nxt := cur + string (c)
269
+ if check (nxt, k) {
270
+ ans = nxt
271
+ q = append (q, nxt)
272
+ }
273
+ }
274
+ }
275
+ return ans
276
+ }
277
+ ```
108
278
279
+ #### TypeScript
280
+
281
+ ``` ts
282
+ function longestSubsequenceRepeatedK(s : string , k : number ): string {
283
+ const check = (t : string , k : number ): boolean => {
284
+ let i = 0 ;
285
+ for (const c of s ) {
286
+ if (c === t [i ]) {
287
+ i ++ ;
288
+ if (i === t .length ) {
289
+ k -- ;
290
+ if (k === 0 ) {
291
+ return true ;
292
+ }
293
+ i = 0 ;
294
+ }
295
+ }
296
+ }
297
+ return false ;
298
+ };
299
+
300
+ const cnt = new Array (26 ).fill (0 );
301
+ for (const c of s ) {
302
+ cnt [c .charCodeAt (0 ) - 97 ]++ ;
303
+ }
304
+
305
+ const cs: string [] = [];
306
+ for (let i = 0 ; i < 26 ; ++ i ) {
307
+ if (cnt [i ] >= k ) {
308
+ cs .push (String .fromCharCode (97 + i ));
309
+ }
310
+ }
311
+
312
+ const q: string [] = [' ' ];
313
+ let ans = ' ' ;
314
+ while (q .length > 0 ) {
315
+ const cur = q .shift ()! ;
316
+ for (const c of cs ) {
317
+ const nxt = cur + c ;
318
+ if (check (nxt , k )) {
319
+ ans = nxt ;
320
+ q .push (nxt );
321
+ }
322
+ }
323
+ }
324
+
325
+ return ans ;
326
+ }
109
327
```
110
328
111
329
<!-- tabs:end -->
0 commit comments