Skip to content

Commit f94db06

Browse files
committed
一刷2800
1 parent 5dad83b commit f94db06

File tree

5 files changed

+107
-38
lines changed

5 files changed

+107
-38
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19626,12 +19626,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
1962619626
//|Medium
1962719627
//|
1962819628

19629-
//|{counter:codes}
19630-
//|{leetcode_base_url}/shortest-string-that-contains-three-strings/[2800. Shortest String That Contains Three Strings^]
19631-
//|{source_base_url}/_2800_ShortestStringThatContainsThreeStrings.java[Java]
19632-
//|{doc_base_url}/2800-shortest-string-that-contains-three-strings.adoc[题解]
19633-
//|Medium
19634-
//|
19629+
|{counter:codes}
19630+
|{leetcode_base_url}/shortest-string-that-contains-three-strings/[2800. Shortest String That Contains Three Strings^]
19631+
|{source_base_url}/_2800_ShortestStringThatContainsThreeStrings.java[Java]
19632+
|{doc_base_url}/2800-shortest-string-that-contains-three-strings.adoc[题解]
19633+
|Medium
19634+
|
1963519635

1963619636
//|{counter:codes}
1963719637
//|{leetcode_base_url}/count-stepping-numbers-in-range/[2801. Count Stepping Numbers in Range^]
Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,48 @@
11
[#2800-shortest-string-that-contains-three-strings]
2-
= 2800. Shortest String That Contains Three Strings
2+
= 2800. 包含三个字符串的最短字符串
33

4-
{leetcode}/problems/shortest-string-that-contains-three-strings/[LeetCode - 2800. Shortest String That Contains Three Strings ^]
4+
https://leetcode.cn/problems/shortest-string-that-contains-three-strings/[LeetCode - 2800. 包含三个字符串的最短字符串 ^]
55

6-
Given three strings `a`, `b`, and `c`, your task is to find a string that has the* minimum* length and contains all three strings as *substrings*.
7-
If there are multiple such strings, return the_ _*lexicographically_ _smallest *one.
6+
给你三个字符串 `a``b``c`,你的任务是找到长度 *最短* 的字符串,且这三个字符串都是它的 *子字符串*
87

9-
Return _a string denoting the answer to the problem._
8+
如果有多个这样的字符串,请你返回 *字典序最小* 的一个。
109

11-
*Notes*
10+
请你返回满足题目要求的字符串。
1211

12+
*注意:*
1313

14-
* A string `a` is *lexicographically smaller* than a string `b` (of the same length) if in the first position where `a` and `b` differ, string `a` has a letter that appears *earlier *in the alphabet than the corresponding letter in `b`.
15-
* A *substring* is a contiguous sequence of characters within a string.
14+
* 两个长度相同的字符串 `a` `b` ,如果在第一个不相同的字符处,`a` 的字母在字母表中比 `b` 的字母 *靠前* ,那么字符串 `a` 比字符串 `b` *字典序小*
15+
* *子字符串* 是一个字符串中一段连续的字符序列。
1616
17+
*示例 1:*
1718

18-
19-
*Example 1:*
19+
....
20+
输入:a = "abc", b = "bca", c = "aaa"
21+
输出:"aaabca"
22+
解释:字符串 "aaabca" 包含所有三个字符串:a = ans[2...4] ,b = ans[3..5] ,c = ans[0..2] 。结果字符串的长度至少为 6 ,且"aaabca" 是字典序最小的一个。
23+
....
2024

21-
[subs="verbatim,quotes"]
22-
----
23-
*Input:* a = "abc", b = "bca", c = "aaa"
24-
*Output:* "aaabca"
25-
*Explanation:* We show that "aaabca" contains all the given strings: a = ans[2...4], b = ans[3..5], c = ans[0..2]. It can be shown that the length of the resulting string would be at least 6 and "aaabca" is the lexicographically smallest one.
26-
----
25+
*示例 2:*
2726

28-
*Example 2:*
27+
....
28+
输入:a = "ab", b = "ba", c = "aba"
29+
输出:"aba"
30+
解释:字符串 "aba" 包含所有三个字符串:a = ans[0..1] ,b = ans[1..2] ,c = ans[0..2] 。由于 c 的长度为 3 ,结果字符串的长度至少为 3 。"aba" 是字典序最小的一个。
31+
....
2932

30-
[subs="verbatim,quotes"]
31-
----
32-
*Input:* a = "ab", b = "ba", c = "aba"
33-
*Output:* "aba"
34-
*Explanation: *We show that the string "aba" contains all the given strings: a = ans[0..1], b = ans[1..2], c = ans[0..2]. Since the length of c is 3, the length of the resulting string would be at least 3. It can be shown that "aba" is the lexicographically smallest one.
35-
36-
----
33+
*提示:*
3734

38-
39-
*Constraints:*
35+
* `+1 <= a.length, b.length, c.length <= 100+`
36+
* `a``b``c` 只包含小写英文字母。
4037
4138
42-
* `1 <= a.length, b.length, c.length <= 100`
43-
* `a`, `b`, `c` consist only of lowercase English letters.
44-
39+
== 思路分析
4540

41+
先使用回溯获取三个字符串的所有全排列,然后在这些全排列中,通过合并字符串首尾相同的部分来获取更短的子串,在这些子串中选择最短,字典排序最小的字符串。
4642

43+
NOTE: 合并字符串时,注意处理首尾的相同子串。
4744

48-
== 思路分析
45+
最初思路是直接合并三个字符,这样会因为没有处理“首尾相同”的子串而让结果变长。
4946

5047

5148
[[src-2800]]
@@ -73,4 +70,4 @@ include::{sourcedir}/_2800_ShortestStringThatContainsThreeStrings.java[tag=answe
7370

7471
== 参考资料
7572

76-
73+
. https://leetcode.cn/problems/shortest-string-that-contains-three-strings/solutions/2364733/mei-ju-by-endlesscheng-qc44/[2800. 包含三个字符串的最短字符串 - 枚举^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5684,7 +5684,7 @@ include::2684-maximum-number-of-moves-in-a-grid.adoc[leveloffset=+1]
56845684

56855685
// include::2799-count-complete-subarrays-in-an-array.adoc[leveloffset=+1]
56865686

5687-
// include::2800-shortest-string-that-contains-three-strings.adoc[leveloffset=+1]
5687+
include::2800-shortest-string-that-contains-three-strings.adoc[leveloffset=+1]
56885688

56895689
// include::2801-count-stepping-numbers-in-range.adoc[leveloffset=+1]
56905690

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,11 @@ endif::[]
648648
|{doc_base_url}/0705-design-hashset.adoc[题解]
649649
|✅ 使用开放地址法解决哈希冲突。
650650

651+
|{counter:codes2503}
652+
|{leetcode_base_url}/shortest-string-that-contains-three-strings/[2800. 包含三个字符串的最短字符串^]
653+
|{doc_base_url}/2800-shortest-string-that-contains-three-strings.adoc[题解]
654+
|❌ 先求三个字符串的全排列,然后合并(合并时注意处理首尾相同子串,减少合并字符串的长度),在这些合并后的字符串中选择最短,字典排序最小的字符串。
655+
651656
|===
652657

653658
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class _2800_ShortestStringThatContainsThreeStrings {
8+
// tag::answer[]
9+
10+
/**
11+
* @author D瓜哥 · https://www.diguage.com
12+
* @since 2025-05-05 22:38:45
13+
*/
14+
public String minimumString(String a, String b, String c) {
15+
List<List<String>> perms = new ArrayList<>(6);
16+
List<String> ss = Arrays.asList(a, b, c);
17+
backtrack(ss, perms, new ArrayList<>(), new boolean[ss.size()]);
18+
String result = null;
19+
for (List<String> perm : perms) {
20+
String temp = merge(merge(perm.get(0), perm.get(1)), perm.get(2));
21+
if (result == null || temp.length() < result.length()
22+
|| temp.length() == result.length() && temp.compareTo(result) < 0) {
23+
result = temp;
24+
}
25+
}
26+
return result;
27+
}
28+
29+
private String merge(String a, String b) {
30+
if (a.contains(b)) return a;
31+
if (b.contains(a)) return b;
32+
int min = Math.min(a.length(), b.length());
33+
for (int i = min; i > 0; i--) {
34+
// 如果 a 的结尾和 b 的开头子串相同,则该子串只需要出现一次
35+
if (a.substring(a.length() - i)
36+
.equals(b.substring(0, i))) {
37+
return a + b.substring(i);
38+
}
39+
}
40+
return a + b;
41+
}
42+
43+
private void backtrack(List<String> ss, List<List<String>> set,
44+
List<String> path, boolean[] used) {
45+
if (ss.size() == path.size()) {
46+
set.add(new ArrayList<>(path));
47+
return;
48+
}
49+
for (int i = 0; i < ss.size(); i++) {
50+
if (used[i]) {
51+
continue;
52+
}
53+
used[i] = true;
54+
path.add(ss.get(i));
55+
backtrack(ss, set, path, used);
56+
path.removeLast();
57+
used[i] = false;
58+
}
59+
}
60+
61+
// end::answer[]
62+
public static void main(String[] args) {
63+
new _2800_ShortestStringThatContainsThreeStrings()
64+
// .merge("abc", "def");
65+
.minimumString("abc", "bca", "aaa");
66+
}
67+
}

0 commit comments

Comments
 (0)