Why it always returns time limited out for my codes


  • 0
    M

    The following is my codes, I really do not why it always returns "time limited out".
    I compile it and run it under my own environment, it shows ok.
    anybody who can help me.

    struct WordDictionary {
        char v;
        struct WordDictionary *trie[26];
    };
    
    struct WordDictionary* wordDictionaryCreate() {
        struct WordDictionary* item = (struct WordDictionary* )malloc(sizeof(struct WordDictionary));
        memset(item, 0, sizeof(struct WordDictionary));
        return item;
    }
    
    void addWord(struct WordDictionary* d, char* word) {
        if(!word || !d)
            return;
    
        struct WordDictionary* p = d;
        int i=0, j = 0;
        int len = strlen(word);
        for(j=0; j<len; j++)
        {
            i = word[j] - 'a';
            if(!p->trie[i])
            {
                p->trie[i] = wordDictionaryCreate();
            }
            p = p->trie[i];
        }
        p->v = 1;
    }
    
    bool search(struct WordDictionary* wordDictionary, char* word) {
        struct WordDictionary* p = wordDictionary;
        if(!word)
            return false;
    
        int j, flag, i = 0;
        char *pend = word + strlen(word);
        while(word<pend)
        {
            i = *word - 'a';
            if(*word=='.')
            {
                int flag = 0;
                for(j=0; j<26; j++)
                {
                    if(!p->trie[j])
                        continue;
    
                    flag = 1;
                    if(search(p->trie[j], word+1)==false)
                        return false;
                }
                if(flag==0)
                    return false;
            }
            else
            {
                p = p->trie[i];
                if(!p)
                    return false;
            }
            word++;
        }
        
        return p->v==1?true:false;
    }
    
    void wordDictionaryFree(struct WordDictionary* wordDictionary) {
        int i = 0;
        struct WordDictionary* p = wordDictionary;
        while(p)
        {
            for(i=0; i<26; i++)
                wordDictionaryFree(p->trie[i]);
        }
        if(p)
            free(p);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.