Can someone tell me whats wrong with my code keep getting RTE


  • 0
    N
    struct Trie
    {
        vector<Trie*> children;
        bool isLeaf;
        Trie()
            :isLeaf(false),children(26,nullptr)
        {}
    };
    class WordDictionary {
    private:
        Trie* root;
    public:
    
        // Adds a word into the data structure.
        void addWord(string& word) {
            auto tmp = root;
            for(int i=0; i<word.length(); i++)
            {
                if(!tmp->children[word[i]-'a'])
                {
                    tmp->children[word[i]-'a'] = new Trie();
                }
                tmp = tmp->children[word[i]-'a'];
            }
            tmp->isLeaf = true;
            cout << word << endl;
        }
    
        // Returns if the word is in the data structure. A word could
        // contain the dot character '.' to represent any one letter.
        bool search(string& word) {
            if(word.length() == 0)
                return false;
            return searchRecur(root, word, 0);
        }
        
        bool searchRecur(Trie* root, string& word, int pos)
        {
            if(root == nullptr)
                return false;
            if(pos == word.length() && root->isLeaf)
                return true;
            if(word[pos]=='.')
            {
                for(int i=0; i<26; i++)
                    if(searchRecur(root->children[i],word,pos+1))
                        return true;
                return false;
            }
            return (root->children[word[pos]-'a'] != nullptr ? searchRecur(root->children[word[pos]-'a'],word,pos+1) : false);
        }
    };
    
    // Your WordDictionary object will be instantiated and called as such:
    // WordDictionary wordDictionary;
    // wordDictionary.addWord("word");
    // wordDictionary.search("pattern");
    

Log in to reply
 

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