Skip to content

Commit

Permalink
add: 统计重复个数
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 2, 2024
1 parent 19aa8dd commit 2a9b73a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文

### 字符串

- [统计重复个数](src/string/count_the_repetitions.cpp) [字符串, 动态规划]

- LeetCode 466. 统计重复个数 <https://leetcode.cn/problems/count-the-repetitions>

- [字典序最小回文串](src/string/lexicographically_smallest_palindrome.cpp) [贪心, 双指针, 字符串]

- LeetCode 2697. 字典序最小回文串 <https://leetcode.cn/problems/lexicographically-smallest-palindrome>
Expand Down
Binary file added images/string/count_the_repetitions.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions src/string/count_the_repetitions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 统计重复个数
// https://leetcode.cn/problems/count-the-repetitions
// INLINE ../../images/string/count_the_repetitions.jpeg

#include <headers.hpp>

class Solution {
public:
int getMaxRepetitions(string s1, int n1, string s2, int n2) {
int len1 = s1.length();
int len2 = s2.length();
int index1 = 0;
int index2 = 0;

if (len1 == 0 || len2 == 0 || len1 * n1 < len2 * n2) {
return 0;
}

unordered_map<int, int> map1;
unordered_map<int, int> map2;
int ans = 0;

while (index1 / len1 < n1) {
if (index1 % len1 == len1 - 1) {
if (map1.count(index2 % len2)) {
int cycleLen = index1 / len1 - map1[index2 % len2] / len1;
int cycleNum = (n1 - 1 - index1 / len1) / cycleLen;
int cycleS2Num = index2 / len2 - map2[index2 % len2] / len2;

index1 += cycleNum * cycleLen * len1;
ans += cycleNum * cycleS2Num;
} else {
map1[index2 % len2] = index1;
map2[index2 % len2] = index2;
}
}

if (s1[index1 % len1] == s2[index2 % len2]) {
if (index2 % len2 == len2 - 1) {
ans += 1;
}
index2 += 1;
}
index1 += 1;
}
return ans / n2;
}
};
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 执行编译时间:2024-01-01 11:39:56
// 执行编译时间:2024-01-02 09:20:33
#include <gtest/gtest.h>
#include <lib.hpp>

Expand Down
22 changes: 22 additions & 0 deletions test/string/count_the_repetitions_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <string/count_the_repetitions.cpp>

TEST(统计重复个数, getMaxRepetitions) {
Solution solution;
// 示例 1:
// 输入:s1 = "acb", n1 = 4, s2 = "ab", n2 = 2
// 输出:2
string s1 = "acb";
int n1 = 4;
string s2 = "ab";
int n2 = 2;
EXPECT_EQ(solution.getMaxRepetitions(s1, n1, s2, n2), 2);

// 示例 2:
// 输入:s1 = "acb", n1 = 1, s2 = "acb", n2 = 1
// 输出:1
s1 = "acb";
n1 = 1;
s2 = "acb";
n2 = 1;
EXPECT_EQ(solution.getMaxRepetitions(s1, n1, s2, n2), 1);
}

0 comments on commit 2a9b73a

Please sign in to comment.