diff --git a/include/solution/1668.h b/include/solution/1668.h new file mode 100644 index 0000000..c5f68a8 --- /dev/null +++ b/include/solution/1668.h @@ -0,0 +1,14 @@ +// This file is generated by mkfile.py +// Date: 2025-12-05 + +#ifndef INC_1668_H +#define INC_1668_H +#ifdef __cplusplus +extern "C" +{ +#endif + int maxRepeating(char *sequence, char *word); +#ifdef __cplusplus +} +#endif +#endif // INC_1668_H diff --git a/src/1668.c b/src/1668.c new file mode 100644 index 0000000..97b72b2 --- /dev/null +++ b/src/1668.c @@ -0,0 +1,37 @@ +// This file is generated by mkfile.py +// Date: 2025-12-05 + +#include +#include +#include + +#define max(x, y) ((x) > (y) ? (x) : (y)) + +int maxRepeating(char *sequence, char *word) +{ + int n = strlen(sequence), m = strlen(word); + if (n < m) + return 0; + + int dp[n]; + for (int i = m - 1; i < n; i++) + { + bool valid = true; + for (int j = 0; j < m; j++) + { + if (sequence[i - m + 1] != word[j]) + { + valid = false; + break; + } + } + if (valid) + dp[i] = (i == m - 1 ? 0 : dp[i - m] + 1); + } + + int ret = dp[0]; + for (int i = 1; i < n; i++) + ret = max(ret, dp[i]); + + return ret; +} \ No newline at end of file diff --git a/tests/test_1668.cpp b/tests/test_1668.cpp new file mode 100644 index 0000000..997e7aa --- /dev/null +++ b/tests/test_1668.cpp @@ -0,0 +1,49 @@ +// This file is generated by mkfile.py +// Date: 2025-12-05 + +#include +#include + +#include + +// Tests for maxRepeating (LeetCode 1668) +class MaxRepeatingTest : public ::testing::Test +{ + protected: + void AssertMaxRepeating(const std::string &sequence, const std::string &word, int expected) + { + int result = maxRepeating(const_cast(sequence.c_str()), const_cast(word.c_str())); + ASSERT_EQ(result, expected); + } +}; + +TEST_F(MaxRepeatingTest, Example1) +{ + AssertMaxRepeating("ababc", "ab", 2); +} + +TEST_F(MaxRepeatingTest, Example2) +{ + AssertMaxRepeating("ababc", "ba", 1); +} + +TEST_F(MaxRepeatingTest, RepeatedMany) +{ + AssertMaxRepeating("abababab", "ab", 4); +} + +TEST_F(MaxRepeatingTest, OverlappingNotAllowed) +{ + // word = "aa", repeated twice is "aaaa" which appears, so answer 2 + AssertMaxRepeating("aaaaa", "aa", 2); +} + +TEST_F(MaxRepeatingTest, ShorterThanWord) +{ + AssertMaxRepeating("a", "aa", 0); +} + +TEST_F(MaxRepeatingTest, SingleCharWord) +{ + AssertMaxRepeating("aaaa", "a", 4); +}