290
This commit is contained in:
12
include/solution/290.h
Normal file
12
include/solution/290.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef INC_290_H
|
||||||
|
#define INC_290_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
#include <stdbool.h>
|
||||||
|
bool wordPattern(char *pattern, char *s);
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
73
src/290.c
Normal file
73
src/290.c
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#include <solution/290.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
62
tests/test_290.cpp
Normal file
62
tests/test_290.cpp
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <solution/290.h>
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user