forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 0211-design-add-and-search-words-data-structure.c
- Loading branch information
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
typedef struct WordDictionary{ | ||
struct WordDictionary *c[26]; | ||
bool isWord; | ||
} WordDictionary; | ||
|
||
WordDictionary *NewDictionary() { | ||
// allocate and init the memory for a dictionary element. | ||
WordDictionary *n = (WordDictionary*)calloc(1, sizeof(WordDictionary)); | ||
return n; | ||
} | ||
|
||
WordDictionary* wordDictionaryCreate() { | ||
WordDictionary *root = NewDictionary(); | ||
return root; | ||
} | ||
|
||
void wordDictionaryAddWord(WordDictionary* obj, char * word) { | ||
assert(obj); | ||
int n = strlen(word); | ||
WordDictionary* dict = obj; | ||
int i, j; | ||
for (i = 0; i < n; i++) { | ||
j = word[i] - 'a'; | ||
if (!dict->c[j]) { | ||
dict->c[j] = NewDictionary(); | ||
} | ||
dict = dict->c[j]; | ||
} | ||
dict->isWord = true; | ||
} | ||
|
||
bool wordDictionarySearchR(WordDictionary* dict, char * word, int index) { | ||
assert(dict); | ||
|
||
char c = word[index]; | ||
int i, j; | ||
if (index == strlen(word)) { | ||
return dict->isWord; | ||
} | ||
if (c == '.') { | ||
for (i = 0; i < 26; i++) { | ||
if (dict->c[i] && wordDictionarySearchR(dict->c[i], word, index + 1)) { | ||
return true; | ||
} | ||
} | ||
} else { | ||
j = c - 'a'; | ||
if (!dict->c[j]) { | ||
return false; | ||
} | ||
return wordDictionarySearchR(dict->c[j], word, index + 1); | ||
} | ||
return false; | ||
} | ||
|
||
bool wordDictionarySearch(WordDictionary* obj, char * word) { | ||
assert(obj); | ||
return wordDictionarySearchR(obj, word, 0); | ||
} | ||
|
||
void wordDictionaryFreeR(WordDictionary* dict) { | ||
assert(dict); | ||
int i; | ||
for (i = 0; i < 26; i++) { | ||
if (dict->c[i]) { | ||
return wordDictionaryFreeR(dict->c[i]); | ||
} | ||
} | ||
free(dict); | ||
} | ||
|
||
void wordDictionaryFree(WordDictionary* obj) { | ||
assert(obj); | ||
wordDictionaryFreeR(obj); | ||
} | ||
|
||
/** | ||
* Your WordDictionary struct will be instantiated and called as such: | ||
* WordDictionary* obj = wordDictionaryCreate(); | ||
* wordDictionaryAddWord(obj, word); | ||
* bool param_2 = wordDictionarySearch(obj, word); | ||
* wordDictionaryFree(obj); | ||
*/ | ||
|