forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0290-Word-Pattern.c
72 lines (58 loc) · 2.01 KB
/
0290-Word-Pattern.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
typedef struct charToWord {
int ch; /* we'll use this field as the key */
char* word;
UT_hash_handle hh; /* makes this structure hashable */
}charToWord;
typedef struct wordToChar {
char* word; /* we'll use this field as the key */
int ch;
UT_hash_handle hh; /* makes this structure hashable */
}wordToChar;
bool wordPattern(char * pattern, char * s){
charToWord* charToWordMap = NULL;
wordToChar* wordToCharMap = NULL;
char* word = NULL;
// Get the first word
word = strtok(s, " ");
for(size_t i = 0; i < strlen(pattern); i++)
{
charToWord* charToWordEntry = NULL;
wordToChar* wordToCharEntry = NULL;
int ch = pattern[i];
// If there is no words left (pattern > s)
if(word == NULL)
{
return false;
}
HASH_FIND_INT(charToWordMap, &ch, charToWordEntry);
HASH_FIND_STR(wordToCharMap, word, wordToCharEntry);
// If the char does exist in the map and the mapping is not the current word
if(charToWordEntry && strcmp(charToWordEntry->word, word) != 0)
{
return false;
}
// If the word does exist in the map and the mapping is not the current char
if(wordToCharEntry && wordToCharEntry->ch != ch)
{
return false;
}
/* Setup hash entries */
charToWordEntry = (charToWord*)malloc(sizeof(charToWord));
charToWordEntry->ch = ch;
charToWordEntry->word = word;
wordToCharEntry = (wordToChar*)malloc(sizeof(wordToChar));
wordToCharEntry->word = word;
wordToCharEntry->ch = ch;
/* Add entries to the hashes */
HASH_ADD_INT(charToWordMap, ch, charToWordEntry);
HASH_ADD_STR(wordToCharMap, word, wordToCharEntry);
// Move to the next word
word = strtok(NULL, " ");
}
// If there is any words left (s > pattern)
if(word != NULL)
{
return false;
}
return true;
}