Skip to content

Commit 0c23459

Browse files
committed
构造限制重复的字符串
1 parent 70f578a commit 0c23459

6 files changed

+83
-2
lines changed

.vscode/settings.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
"aaabaaa",
148148
"aaabbaaa",
149149
"AAABBC",
150+
"aababab",
150151
"aabb",
151152
"aabcbc",
152153
"aadsfasf",
@@ -174,11 +175,14 @@
174175
"backgrounding",
175176
"Barcodes",
176177
"bbab",
178+
"bbabaa",
179+
"bbabaaa",
177180
"bdca",
178181
"binutils",
179182
"breakpad",
180183
"cadsfafs",
181184
"camelcase",
185+
"cczazcc",
182186
"Choco",
183187
"clangd",
184188
"cmake",
@@ -263,6 +267,8 @@
263267
"XRLXXRRLX",
264268
"XRLXXRRXL",
265269
"XRXLRXRXL",
266-
"zaaaz"
270+
"zaaaz",
271+
"zzcccac",
272+
"zzcccca"
267273
]
268274
}

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文
152152

153153
### 字符串
154154

155+
- [构造限制重复的字符串](src/string/construct_string_with_repeat_limit.cpp) [贪心, 字符串, 计数, 堆(优先队列)]
156+
157+
- LeetCode 2182. 构造限制重复的字符串 <https://leetcode.cn/problems/construct-string-with-repeat-limit>
158+
155159
- [统计重复个数](src/string/count_the_repetitions.cpp) [字符串, 动态规划]
156160

157161
- LeetCode 466. 统计重复个数 <https://leetcode.cn/problems/count-the-repetitions>
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 构造限制重复的字符串
2+
// https://leetcode.cn/problems/construct-string-with-repeat-limit
3+
// INLINE ../../images/string/construct_string_with_repeat_limit.jpeg
4+
// 参考官方题解
5+
6+
#include <headers.hpp>
7+
8+
class Solution {
9+
public:
10+
string repeatLimitedString(string s, int repeatLimit) {
11+
int N = 26;
12+
vector<int> count(N);
13+
for (char c : s) {
14+
count[c - 'a']++;
15+
}
16+
string ret;
17+
int m = 0;
18+
for (int i = N - 1, j = N - 2; i >= 0 && j >= 0;) {
19+
if (count[i] == 0) { // 当前字符已经填完,填入后面的字符,重置 m
20+
m = 0;
21+
i--;
22+
} else if (m < repeatLimit) { // 当前字符未超过限制
23+
count[i]--;
24+
ret.push_back('a' + i);
25+
m++;
26+
} else if (j >= i ||
27+
count[j] == 0) { // 当前字符已经超过限制,查找可填入的其他字符
28+
j--;
29+
} else { // 当前字符已经超过限制,填入其他字符,并且重置 m
30+
count[j]--;
31+
ret.push_back('a' + j);
32+
m = 0;
33+
}
34+
}
35+
return ret;
36+
}
37+
};

test/lib/lib_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// 执行编译时间:2024-01-12 11:35:49
1+
// 执行编译时间:2024-01-13 14:28:48
22
#include <gtest/gtest.h>
33
#include <lib.hpp>
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <string/construct_string_with_repeat_limit.cpp>
2+
3+
TEST(构造限制重复的字符串, repeatLimitedString) {
4+
Solution solution;
5+
// 示例 1:
6+
// 输入:s = "cczazcc", repeatLimit = 3
7+
// 输出:"zzcccac"
8+
// 解释:使用 s 中的所有字符来构造 repeatLimitedString "zzcccac"。
9+
// 字母 'a' 连续出现至多 1 次。
10+
// 字母 'c' 连续出现至多 3 次。
11+
// 字母 'z' 连续出现至多 2 次。
12+
// 因此,没有字母连续出现超过 repeatLimit 次,字符串是一个有效的
13+
// repeatLimitedString 。 该字符串是字典序最大的 repeatLimitedString
14+
// ,所以返回 "zzcccac" 。 注意,尽管 "zzcccca" 字典序更大,但字母 'c'
15+
// 连续出现超过 3 次,所以它不是一个有效的 repeatLimitedString 。
16+
string s = "cczazcc";
17+
int repeatLimit = 3;
18+
EXPECT_EQ(solution.repeatLimitedString(s, repeatLimit), "zzcccac");
19+
20+
// 示例 2:
21+
// 输入:s = "aababab", repeatLimit = 2
22+
// 输出:"bbabaa"
23+
// 使用 s 中的一些字符来构造 repeatLimitedString "bbabaa"。
24+
// 解释:
25+
// 字母 'a' 连续出现至多 2 次。
26+
// 字母 'b' 连续出现至多 2 次。
27+
// 因此,没有字母连续出现超过 repeatLimit 次,字符串是一个有效的
28+
// repeatLimitedString 。 该字符串是字典序最大的 repeatLimitedString
29+
// ,所以返回 "bbabaa" 。 注意,尽管 "bbabaaa" 字典序更大,但字母 'a'
30+
// 连续出现超过 2 次,所以它不是一个有效的 repeatLimitedString 。
31+
s = "aababab";
32+
repeatLimit = 2;
33+
EXPECT_EQ(solution.repeatLimitedString(s, repeatLimit), "bbabaa");
34+
}

0 commit comments

Comments
 (0)