1668
This commit is contained in:
14
include/solution/1668.h
Normal file
14
include/solution/1668.h
Normal file
@@ -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
|
||||
37
src/1668.c
Normal file
37
src/1668.c
Normal file
@@ -0,0 +1,37 @@
|
||||
// This file is generated by mkfile.py
|
||||
// Date: 2025-12-05
|
||||
|
||||
#include <solution/1668.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
49
tests/test_1668.cpp
Normal file
49
tests/test_1668.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
// This file is generated by mkfile.py
|
||||
// Date: 2025-12-05
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <solution/1668.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
// 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<char *>(sequence.c_str()), const_cast<char *>(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);
|
||||
}
|
||||
Reference in New Issue
Block a user