This commit is contained in:
Jeffrey Hsu 2025-03-28 20:56:16 +08:00
parent c69a6b5ca9
commit 5f09521d43
3 changed files with 75 additions and 0 deletions

15
include/solution/2716.h Normal file
View File

@ -0,0 +1,15 @@
//
// Created by xfj12 on 2025/3/28.
//
#ifndef INC_2716_H
#define INC_2716_H
#ifdef __cplusplus
extern "C"
{
#endif
int minimizedStringLength(char *s);
#ifdef __cplusplus
}
#endif
#endif // INC_2716_H

26
src/2716.c Normal file
View File

@ -0,0 +1,26 @@
//
// Created by xfj12 on 2025/3/28.
//
#include <solution/2716.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
int minimizedStringLength(char *s)
{
int n = strlen(s);
int size = 0;
char *temp = malloc(n);
for (int i = 0; i < n; i++)
{
bool flag = true;
for (int j = 0; j < size && flag; j++)
if (s[i] == temp[j])
flag = false;
if (flag)
temp[size++] = s[i];
}
free(temp);
return size;
}

34
tests/test_2716.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <gtest/gtest.h>
#include <solution/2716.h>
// 测试用例 1
TEST(MinimizedStringLengthTest, Test1)
{
char s[] = "aaabc";
int expected = 3;
EXPECT_EQ(minimizedStringLength(s), expected);
}
// 测试用例 2
TEST(MinimizedStringLengthTest, Test2)
{
char s[] = "cbbd";
int expected = 3;
EXPECT_EQ(minimizedStringLength(s), expected);
}
// 测试用例 3
TEST(MinimizedStringLengthTest, Test3)
{
char s[] = "dddaaa";
int expected = 2;
EXPECT_EQ(minimizedStringLength(s), expected);
}
// 测试用例 4
TEST(MinimizedStringLengthTest, Test4)
{
char s[] = "baadccab";
int expected = 4;
EXPECT_EQ(minimizedStringLength(s), expected);
}