forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
211.c
99 lines (81 loc) · 2.84 KB
/
211.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
struct WordDictionary {
bool end;
struct WordDictionary *child[26]; /* a - z */
};
/** Initialize your data structure here. */
struct WordDictionary* wordDictionaryCreate() {
struct WordDictionary *new_node
= (struct WordDictionary *)malloc(sizeof(struct WordDictionary));
new_node->end = false;
memset(new_node->child, 0, sizeof(new_node->child));
return new_node;
}
/** Inserts a word into the data structure. */
void addWord(struct WordDictionary* wordDictionary, char* word) {
if (wordDictionary == NULL || word == NULL) return;
struct WordDictionary *p = wordDictionary;
while (*word != '\0') {
if (p->child[*word - 'a'] == NULL) {
p->child[*word - 'a'] = wordDictionaryCreate();
}
p = p->child[*word - 'a'];
word++;
}
p->end = true;
}
/** Returns if the word is in the data structure. A word could
contain the dot character '.' to represent any one letter. */
bool search(struct WordDictionary* wordDictionary, char* word) {
if (wordDictionary == NULL || word == NULL) return false;
if (*word == '\0') {
if (wordDictionary->end) return true;
else return false;
}
int i;
if (*word == '.') {
for (i = 0; i < 26; i++) {
if (wordDictionary->child[i]) {
bool flag = search(wordDictionary->child[i], word + 1);
if (flag) return true;
}
}
return false;
}
else {
return search(wordDictionary->child[*word - 'a'], word + 1);
}
}
/** Deallocates memory previously allocated for the data structure. */
void wordDictionaryFree(struct WordDictionary* wordDictionary) {
if (wordDictionary == NULL) return;
int i;
for (i = 0; i < 26; i++) {
wordDictionaryFree(wordDictionary->child[i]);
}
free(wordDictionary);
}
int main() {
struct WordDictionary* wordDictionary = wordDictionaryCreate();
addWord(wordDictionary, "bad");
addWord(wordDictionary, "dad");
addWord(wordDictionary, "mad");
addWord(wordDictionary, "dcd");
assert(search(wordDictionary, "pad") == false);
assert(search(wordDictionary, "bad") == true);
assert(search(wordDictionary, ".ad") == true);
assert(search(wordDictionary, "b..") == true);
assert(search(wordDictionary, "b.d") == true);
assert(search(wordDictionary, "b.c") == false);
assert(search(wordDictionary, ".cd") == true);
assert(search(wordDictionary, "b") == false);
assert(search(wordDictionary, ".") == false);
assert(search(wordDictionary, "badd") == false);
wordDictionaryFree(wordDictionary);
printf("all test pased!\n");
return 0;
}