LeetCode Add and Search Word, leetcodeadd

Source: Internet
Author: User

LeetCode Add and Search Word, leetcodeadd

Question

Ideas
The prefix tree contains similar LeetCode questions. I modified it with the previous code.

Code

struct WordDictionary {    char c;    // sons for "abcdefghijklmnopqrstuvwxyz\0"    struct WordDictionary * son[27];  };/** Initialize your data structure here. */struct WordDictionary* wordDictionaryCreate() {    struct WordDictionary * WordDictionary =         (struct WordDictionary*)malloc(sizeof(struct WordDictionary));    WordDictionary->c = '\0';    memset(WordDictionary->son, 0, sizeof(WordDictionary->son));    return WordDictionary;}/** Inserts a word into the data structure. */void addWord(struct WordDictionary * wordDictionary, char * word) {    if (*word == '\0') {        wordDictionary->son[26] = wordDictionaryCreate();        // notice that '\0' is important.         // There's "abc\0" in Trie doesn't mean there's a word "ab\0".        wordDictionary->son[26]->c = '\0';          return;    }    if (wordDictionary->son[*word - 'a'] == NULL) {        wordDictionary->son[*word - 'a'] = wordDictionaryCreate();        wordDictionary->son[*word - 'a']->c = *word;        addWord(wordDictionary->son[*word - 'a'], word + 1);    }    else {        addWord(wordDictionary->son[*word - 'a'], word + 1);    }}/** Returns if the word is in the data structure. A word couldcontain the dot character '.' to represent any one letter. */bool search(struct WordDictionary * wordDictionary, char * word) {    if (*word == '\0') {        if (wordDictionary->son[26] != NULL) return true;        else return false;    }    if (*word == '.') {        for (int i = 0; i < 26; i++) {            if (wordDictionary->son[i] != NULL) {                if (search(wordDictionary->son[i], word + 1))                    return true;            }        }    }    else {        if (wordDictionary->son[*word - 'a'] == NULL) {            return false;        }        else {            return search(wordDictionary->son[*word - 'a'], word + 1);        }    }}/** Deallocates memory previously allocated for the data structure. */void wordDictionaryFree(struct WordDictionary * wordDictionary) {    if (wordDictionary != NULL) {        for (int i = 0; i < 26; i++) {            wordDictionaryFree(wordDictionary->son[i]);        }        free(wordDictionary);    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.