diff --git a/include/solution/290.h b/include/solution/290.h new file mode 100644 index 0000000..cd6e75b --- /dev/null +++ b/include/solution/290.h @@ -0,0 +1,12 @@ +#ifndef INC_290_H +#define INC_290_H +#ifdef __cplusplus +extern "C" +{ +#endif +#include + bool wordPattern(char *pattern, char *s); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/src/290.c b/src/290.c new file mode 100644 index 0000000..9d76337 --- /dev/null +++ b/src/290.c @@ -0,0 +1,73 @@ +#include +#include +#include + +char *foundValue(char **d, char k) +{ + int idx = k - 'a'; + return d[idx]; +} + +int foundKeyCount(char **d, char *v) +{ + int count = 0; + for (int i = 0; i < 26; i++) + if (d[i] && strcmp(v, d[i]) == 0) + count++; + + return count; +} + +void freeDict(char **d) +{ + for (int i = 0; i < 26; i++) + free(d[i]); + free(d); +} + +int insertValue(char **d, char k, char *v) +{ + int idx = k - 'a'; + if (d[idx] != NULL) + return -1; + d[idx] = malloc(strlen(v) + 1); + memcpy(d[idx], v, strlen(v) + 1); + + return foundKeyCount(d, v); +} + +bool preCheck(char *p, char *s) +{ + int s_count = 0; + for (; *s != '\0'; s++) + if (*s == ' ') + s_count++; + return s_count + 1 == strlen(p); +} + +bool wordPattern(char *pattern, char *s) +{ + if (!preCheck(pattern, s)) + return false; + + char **d = calloc(26, sizeof(char *)); + char *token = strtok(s, " "); + + for (int i = 0; i < strlen(pattern) && token != NULL; i++, token = strtok(NULL, " ")) + { + switch (insertValue(d, pattern[i], token)) + { + case 1: + break; + case -1: + if (strcmp(token, foundValue(d, pattern[i])) == 0) + break; + default: + freeDict(d); + return false; + } + } + + freeDict(d); + return true; +} \ No newline at end of file diff --git a/tests/test_290.cpp b/tests/test_290.cpp new file mode 100644 index 0000000..d572b65 --- /dev/null +++ b/tests/test_290.cpp @@ -0,0 +1,62 @@ +#include +#include + +class WordPatternTest : public ::testing::Test +{ +}; + +// 示例 1: 输入 pattern = "abba", s = "dog cat cat dog" 输出 true +TEST_F(WordPatternTest, Example1) +{ + char pattern[] = "abba"; + char s[] = "dog cat cat dog"; + EXPECT_TRUE(wordPattern(pattern, s)); +} + +// 示例 2: 输入 pattern = "abba", s = "dog cat cat fish" 输出 false +TEST_F(WordPatternTest, Example2) +{ + char pattern[] = "abba"; + char s[] = "dog cat cat fish"; + EXPECT_FALSE(wordPattern(pattern, s)); +} + +// 示例 3: 输入 pattern = "aaaa", s = "dog cat cat dog" 输出 false +TEST_F(WordPatternTest, Example3) +{ + char pattern[] = "aaaa"; + char s[] = "dog cat cat dog"; + EXPECT_FALSE(wordPattern(pattern, s)); +} + +// 示例 4: 输入 pattern = "abba", s = "dog dog dog dog" 输出 false +TEST_F(WordPatternTest, Example4) +{ + char pattern[] = "abba"; + char s[] = "dog dog dog dog"; + EXPECT_FALSE(wordPattern(pattern, s)); +} + +// 示例 5: 输入 pattern = "abc", s = "dog cat dog" 输出 false +TEST_F(WordPatternTest, Example5) +{ + char pattern[] = "abc"; + char s[] = "dog cat dog"; + EXPECT_FALSE(wordPattern(pattern, s)); +} + +// 示例 6: 输入 pattern = "aaa", s = "aa aa aa aa" 输出 false +TEST_F(WordPatternTest, Example6) +{ + char pattern[] = "aaa"; + char s[] = "aa aa aa aa"; + EXPECT_FALSE(wordPattern(pattern, s)); +} + +// 示例 7: 输入 pattern = "e", s = "eukera" 输出 false +TEST_F(WordPatternTest, Example7) +{ + char pattern[] = "e"; + char s[] = "eukera"; + EXPECT_TRUE(wordPattern(pattern, s)); +} \ No newline at end of file