Skip to content

Commit 41e6c52

Browse files
committed
一刷1209
1 parent 7ef9d8e commit 41e6c52

File tree

5 files changed

+111
-46
lines changed

5 files changed

+111
-46
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8488,14 +8488,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
84888488
//|{doc_base_url}/1208-get-equal-substrings-within-budget.adoc[题解]
84898489
//|Medium
84908490
//|
8491-
//
8492-
//|{counter:codes}
8493-
//|{leetcode_base_url}/remove-all-adjacent-duplicates-in-string-ii/[1209. Remove All Adjacent Duplicates in String II^]
8494-
//|{source_base_url}/_1209_RemoveAllAdjacentDuplicatesInStringII.java[Java]
8495-
//|{doc_base_url}/1209-remove-all-adjacent-duplicates-in-string-ii.adoc[题解]
8496-
//|Medium
8497-
//|
8498-
//
8491+
8492+
|{counter:codes}
8493+
|{leetcode_base_url}/remove-all-adjacent-duplicates-in-string-ii/[1209. Remove All Adjacent Duplicates in String II^]
8494+
|{source_base_url}/_1209_RemoveAllAdjacentDuplicatesInStringII.java[Java]
8495+
|{doc_base_url}/1209-remove-all-adjacent-duplicates-in-string-ii.adoc[题解]
8496+
|Medium
8497+
|
8498+
84998499
//|{counter:codes}
85008500
//|{leetcode_base_url}/minimum-moves-to-reach-target-with-rotations/[1210. Minimum Moves to Reach Target with Rotations^]
85018501
//|{source_base_url}/_1210_MinimumMovesToReachTargetWithRotations.java[Java]
Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,76 @@
11
[#1209-remove-all-adjacent-duplicates-in-string-ii]
2-
= 1209. Remove All Adjacent Duplicates in String II
2+
= 1209. 删除字符串中的所有相邻重复项 II
33

4-
{leetcode}/problems/remove-all-adjacent-duplicates-in-string-ii/[LeetCode - Remove All Adjacent Duplicates in String II^]
4+
https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string-ii/[LeetCode - 1209. 删除字符串中的所有相邻重复项 II ^]
55

6-
Given a string `s`, a _k_ _duplicate removal_ consists of choosing `k` adjacent and equal letters from `s` and removing them causing the left and the right side of the deleted substring to concatenate together.
6+
给你一个字符串 `s`,「`k` 倍重复项删除操作」将会从 `s` 中选择 `k` 个相邻且相等的字母,并删除它们,使被删去的字符串的左侧和右侧连在一起。
77

8-
We repeatedly make `k` duplicate removals on `s` until we no longer can.
8+
你需要对 `s` 重复进行无限次这样的删除操作,直到无法继续为止。
99

10-
Return the final string after all such duplicate removals have been made.
10+
在执行完所有删除操作后,返回最终得到的字符串。
1111

12-
It is guaranteed that the answer is unique.
12+
本题答案保证唯一。
1313

14-
15-
*Example 1:*
14+
*示例 1:*
1615

17-
[subs="verbatim,quotes,macros"]
18-
----
19-
*Input:* s = "abcd", k = 2
20-
*Output:* "abcd"
21-
*Explanation:* There's nothing to delete.
22-
----
16+
....
17+
输入:s = "abcd", k = 2
18+
输出:"abcd"
19+
解释:没有要删除的内容。
20+
....
2321

24-
*Example 2:*
22+
*示例 2:*
2523

26-
[subs="verbatim,quotes,macros"]
27-
----
28-
*Input:* s = "deeedbbcccbdaa", k = 3
29-
*Output:* "aa"
30-
*Explanation:
31-
*First delete "eee" and "ccc", get "ddbbbdaa"
32-
Then delete "bbb", get "dddaa"
33-
Finally delete "ddd", get "aa"
34-
----
24+
....
25+
输入:s = "deeedbbcccbdaa", k = 3
26+
输出:"aa"
27+
解释:
28+
先删除 "eee" 和 "ccc",得到 "ddbbbdaa"
29+
再删除 "bbb",得到 "dddaa"
30+
最后删除 "ddd",得到 "aa"
31+
....
3532

36-
*Example 3:*
33+
*示例 3:*
3734

38-
[subs="verbatim,quotes,macros"]
39-
----
40-
*Input:* s = "pbbcggttciiippooaais", k = 2
41-
*Output:* "ps"
35+
....
36+
输入:s = "pbbcggttciiippooaais", k = 2
37+
输出:"ps"
38+
....
4239

43-
----
4440

45-
46-
*Constraints:*
41+
*提示:*
4742

48-
49-
* `1 <= s.length <= 10^5`
50-
* `2 <= k <= 10^4`
51-
* `s` only contains lower case English letters.
43+
* `1 \<= s.length \<= 10^5^`
44+
* `2 \<= k \<= 10^4^`
45+
* `s` 中只含有小写英文字母。
5246
5347
48+
== 思路分析
5449

5550

5651
[[src-1209]]
52+
[tabs]
53+
====
54+
一刷::
55+
+
56+
--
5757
[{java_src_attr}]
5858
----
59-
include::{sourcedir}/_1209_RemoveAllAdjacentDuplicatesInStringII.java[tag=answer]
59+
include::{sourcedir}/_1209_RemoveAllAdjacentDuplicatesInStringIi.java[tag=answer]
6060
----
61+
--
62+
63+
// 二刷::
64+
// +
65+
// --
66+
// [{java_src_attr}]
67+
// ----
68+
// include::{sourcedir}/_1209_RemoveAllAdjacentDuplicatesInStringIi_2.java[tag=answer]
69+
// ----
70+
// --
71+
====
72+
73+
74+
== 参考资料
75+
6176

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2501,7 +2501,7 @@ include::1189-maximum-number-of-balloons.adoc[leveloffset=+1]
25012501

25022502
// include::1208-get-equal-substrings-within-budget.adoc[leveloffset=+1]
25032503

2504-
// include::1209-remove-all-adjacent-duplicates-in-string-ii.adoc[leveloffset=+1]
2504+
include::1209-remove-all-adjacent-duplicates-in-string-ii.adoc[leveloffset=+1]
25052505

25062506
// include::1210-minimum-moves-to-reach-target-with-rotations.adoc[leveloffset=+1]
25072507

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,11 @@ endif::[]
893893
|{doc_base_url}/1007-minimum-domino-rotations-for-equal-row.adoc[题解]
894894
|✅ 统计每个数字的下标,然后逐个检查每个数字的下标集合是否能完整覆盖原始数字的全部下标。更优解:判断是否可以都变成 `tops[0]` 或者 `bottoms[0]`
895895

896+
|{counter:codes2503}
897+
|{leetcode_base_url}/remove-all-adjacent-duplicates-in-string-ii/[1209. 删除字符串中的所有相邻重复项 II^]
898+
|{doc_base_url}/1209-remove-all-adjacent-duplicates-in-string-ii.adoc[题解]
899+
|✅ 暴力解法:对重复的相邻字母计数,当计数达到 k 时将其删除。重复此操作,直到没有删除的字符为止。通过 19 / 21 个测试用例。
900+
896901

897902
|===
898903

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _1209_RemoveAllAdjacentDuplicatesInStringIi {
4+
// tag::answer[]
5+
6+
/**
7+
* 暴力解法: 通过 19 / 21 个测试用例。
8+
*
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-06-02 07:27:39
11+
*/
12+
public String removeDuplicates(String s, int k) {
13+
if (s == null || s.length() < k) {
14+
return s;
15+
}
16+
StringBuilder sb = new StringBuilder(s);
17+
int length = sb.length();
18+
int index = 0;
19+
while (index < length) {
20+
char c = sb.charAt(index);
21+
int fast = index + 1;
22+
while (fast < length && c == sb.charAt(fast) && fast - index < k) {
23+
fast++;
24+
}
25+
int dc = fast - index;
26+
if (fast <= length && dc >= k) {
27+
sb.replace(index, fast, "");
28+
length = sb.length();
29+
} else {
30+
index = fast;
31+
}
32+
}
33+
if (sb.length() == s.length()) {
34+
return s;
35+
} else {
36+
return removeDuplicates(sb.toString(), k);
37+
}
38+
}
39+
40+
// end::answer[]
41+
public static void main(String[] args) {
42+
new _1209_RemoveAllAdjacentDuplicatesInStringIi()
43+
.removeDuplicates("yfttttfbbbbnnnnffbgffffgbbbbgssssgthyyyy", 4);
44+
}
45+
}

0 commit comments

Comments
 (0)