From 5f7ef7b880498380e1b0cdb5ad69b171510492d3 Mon Sep 17 00:00:00 2001 From: Jeffrey Hsu Date: Wed, 23 Jul 2025 20:19:00 +0800 Subject: [PATCH] 1957 --- include/solution/1957.h | 11 +++++++++++ src/1957.c | 13 +++++++++++++ tests/test_1957.cpp | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 include/solution/1957.h create mode 100644 src/1957.c create mode 100644 tests/test_1957.cpp diff --git a/include/solution/1957.h b/include/solution/1957.h new file mode 100644 index 0000000..dbd6517 --- /dev/null +++ b/include/solution/1957.h @@ -0,0 +1,11 @@ +#ifndef INC_1957_H +#define INC_1957_H +#ifdef __cplusplus +extern "C" +{ +#endif + char *makeFancyString(char *s); +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/1957.c b/src/1957.c new file mode 100644 index 0000000..87387f8 --- /dev/null +++ b/src/1957.c @@ -0,0 +1,13 @@ +#include +#include + +char *makeFancyString(char *s) +{ + int d[26] = {0}; + int len = strlen(s); + for (int i = len - 1; i >= 0; i--) + if (++(d[s[i] - 'a']) > 3) + for (int j = i; j < len - 1; j++) + s[j] = s[j + 1]; + return s; +} \ No newline at end of file diff --git a/tests/test_1957.cpp b/tests/test_1957.cpp new file mode 100644 index 0000000..c4dd986 --- /dev/null +++ b/tests/test_1957.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +class MakeFancyStringTest : public ::testing::Test +{ + protected: + void AssertFancyString(const std::string &input, const std::string &expected) + { + char *c_input = strdup(input.c_str()); // 复制一份以防原始修改 + char *result = makeFancyString(c_input); + + ASSERT_STREQ(result, expected.c_str()); + + // 如函数返回的是 malloc 分配的,则应释放 + // free(result); + free(c_input); + } +}; + +// 示例 1:leeetcode → leetcode +TEST_F(MakeFancyStringTest, Example1) +{ + AssertFancyString("leeetcode", "leetcode"); +} + +// 示例 2:aaabaaaa → aabaa +TEST_F(MakeFancyStringTest, Example2) +{ + AssertFancyString("aaabaaaa", "aabaa"); +} + +// 示例 3:aab → aab +TEST_F(MakeFancyStringTest, Example3) +{ + AssertFancyString("aab", "aab"); +} \ No newline at end of file