@@ -288,4 +288,230 @@ impl Solution {
288
288
289
289
<!-- solution:end -->
290
290
291
+ <!-- solution:start -->
292
+
293
+ ### 方法二:枚举 + KMP
294
+
295
+ 我们可以使用 KMP 算法来优化字符串的合并过程。
296
+
297
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是三个字符串的长度之和。
298
+
299
+ <!-- tabs:start -->
300
+
301
+ #### Python3
302
+
303
+ ``` python
304
+ class Solution :
305
+ def minimumString (self , a : str , b : str , c : str ) -> str :
306
+ def f (s : str , t : str ) -> str :
307
+ if s in t:
308
+ return t
309
+ if t in s:
310
+ return s
311
+ p = t + " #" + s + " $"
312
+ n = len (p)
313
+ next = [0 ] * n
314
+ next [0 ] = - 1
315
+ i, j = 2 , 0
316
+ while i < n:
317
+ if p[i - 1 ] == p[j]:
318
+ j += 1
319
+ next [i] = j
320
+ i += 1
321
+ elif j:
322
+ j = next [j]
323
+ else :
324
+ next [i] = 0
325
+ i += 1
326
+ return s + t[next [- 1 ] :]
327
+
328
+ ans = " "
329
+ for a, b, c in permutations((a, b, c)):
330
+ s = f(f(a, b), c)
331
+ if ans == " " or len (s) < len (ans) or (len (s) == len (ans) and s < ans):
332
+ ans = s
333
+ return ans
334
+ ```
335
+
336
+ #### Java
337
+
338
+ ``` java
339
+ class Solution {
340
+ public String minimumString (String a , String b , String c ) {
341
+ String [] s = {a, b, c};
342
+ int [][] perm = {{0 , 1 , 2 }, {0 , 2 , 1 }, {1 , 0 , 2 }, {1 , 2 , 0 }, {2 , 1 , 0 }, {2 , 0 , 1 }};
343
+ String ans = " " ;
344
+ for (var p : perm) {
345
+ int i = p[0 ], j = p[1 ], k = p[2 ];
346
+ String t = f(f(s[i], s[j]), s[k]);
347
+ if (" " . equals(ans) || t. length() < ans. length()
348
+ || (t. length() == ans. length() && t. compareTo(ans) < 0 )) {
349
+ ans = t;
350
+ }
351
+ }
352
+ return ans;
353
+ }
354
+
355
+ private String f (String s , String t ) {
356
+ if (s. contains(t)) {
357
+ return s;
358
+ }
359
+ if (t. contains(s)) {
360
+ return t;
361
+ }
362
+ char [] p = (t + " #" + s + " $" ). toCharArray();
363
+ int n = p. length;
364
+ int [] next = new int [n];
365
+ next[0 ] = - 1 ;
366
+ for (int i = 2 , j = 0 ; i < n;) {
367
+ if (p[i - 1 ] == p[j]) {
368
+ next[i++ ] = ++ j;
369
+ } else if (j > 0 ) {
370
+ j = next[j];
371
+ } else {
372
+ next[i++ ] = 0 ;
373
+ }
374
+ }
375
+ return s + t. substring(next[n - 1 ]);
376
+ }
377
+ }
378
+ ```
379
+
380
+ #### C++
381
+
382
+ ``` cpp
383
+ class Solution {
384
+ public:
385
+ string minimumString(string a, string b, string c) {
386
+ vector<string > s = {a, b, c};
387
+ vector<vector<int >> perm = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 1, 0}, {2, 0, 1}};
388
+ string ans = "";
389
+ for (auto& p : perm) {
390
+ int i = p[ 0] , j = p[ 1] , k = p[ 2] ;
391
+ string t = f(f(s[ i] , s[ j] ), s[ k] );
392
+ if (ans == "" || t.size() < ans.size() || (t.size() == ans.size() && t < ans)) {
393
+ ans = t;
394
+ }
395
+ }
396
+ return ans;
397
+ }
398
+
399
+ string f(string s, string t) {
400
+ if (s.find(t) != string::npos) {
401
+ return s;
402
+ }
403
+ if (t.find(s) != string::npos) {
404
+ return t;
405
+ }
406
+ string p = t + "#" + s + "$";
407
+ int n = p.size();
408
+ int next[n];
409
+ next[0] = -1;
410
+ next[1] = 0;
411
+ for (int i = 2, j = 0; i < n;) {
412
+ if (p[i - 1] == p[j]) {
413
+ next[i++] = ++j;
414
+ } else if (j > 0) {
415
+ j = next[j];
416
+ } else {
417
+ next[i++] = 0;
418
+ }
419
+ }
420
+ return s + t.substr(next[n - 1]);
421
+ };
422
+ };
423
+ ```
424
+
425
+ #### Go
426
+
427
+ ``` go
428
+ func minimumString (a string , b string , c string ) string {
429
+ f := func (s, t string ) string {
430
+ if strings.Contains (s, t) {
431
+ return s
432
+ }
433
+ if strings.Contains (t, s) {
434
+ return t
435
+ }
436
+ p := t + " #" + s + " $"
437
+ n := len (p)
438
+ next := make ([]int , n)
439
+ next[0 ] = -1
440
+ for i , j := 2 , 0 ; i < n; {
441
+ if p[i-1 ] == p[j] {
442
+ j++
443
+ next[i] = j
444
+ i++
445
+ } else if j > 0 {
446
+ j = next[j]
447
+ } else {
448
+ next[i] = 0
449
+ i++
450
+ }
451
+ }
452
+ return s + t[next[n-1 ]:]
453
+ }
454
+ s := [3 ]string {a, b, c}
455
+ ans := " "
456
+ for _ , p := range [][]int {{0 , 1 , 2 }, {0 , 2 , 1 }, {1 , 0 , 2 }, {1 , 2 , 0 }, {2 , 0 , 1 }, {2 , 1 , 0 }} {
457
+ i , j , k := p[0 ], p[1 ], p[2 ]
458
+ t := f (f (s[i], s[j]), s[k])
459
+ if ans == " " || len (t) < len (ans) || (len (t) == len (ans) && t < ans) {
460
+ ans = t
461
+ }
462
+ }
463
+ return ans
464
+ }
465
+ ```
466
+
467
+ #### TypeScript
468
+
469
+ ``` ts
470
+ function minimumString(a : string , b : string , c : string ): string {
471
+ const f = (s : string , t : string ): string => {
472
+ if (s .includes (t )) {
473
+ return s ;
474
+ }
475
+ if (t .includes (s )) {
476
+ return t ;
477
+ }
478
+ const p = t + ' #' + s + ' $' ;
479
+ const n = p .length ;
480
+ const next: number [] = Array (n ).fill (0 );
481
+ next [0 ] = - 1 ;
482
+ for (let i = 2 , j = 0 ; i < n ; ) {
483
+ if (p [i - 1 ] === p [j ]) {
484
+ next [i ++ ] = ++ j ;
485
+ } else if (j ) {
486
+ j = next [j ];
487
+ } else {
488
+ next [i ++ ] = 0 ;
489
+ }
490
+ }
491
+ return s + t .slice (next [n - 1 ]);
492
+ };
493
+ const s: string [] = [a , b , c ];
494
+ const perm: number [][] = [
495
+ [0 , 1 , 2 ],
496
+ [0 , 2 , 1 ],
497
+ [1 , 0 , 2 ],
498
+ [1 , 2 , 0 ],
499
+ [2 , 0 , 1 ],
500
+ [2 , 1 , 0 ],
501
+ ];
502
+ let ans = ' ' ;
503
+ for (const [i, j, k] of perm ) {
504
+ const t = f (f (s [i ], s [j ]), s [k ]);
505
+ if (ans === ' ' || t .length < ans .length || (t .length === ans .length && t < ans )) {
506
+ ans = t ;
507
+ }
508
+ }
509
+ return ans ;
510
+ }
511
+ ```
512
+
513
+ <!-- tabs:end -->
514
+
515
+ <!-- solution:end -->
516
+
291
517
<!-- problem:end -->
0 commit comments