A concise PrefixTrie based c++ solution.


  • -2
    R
    #include <stdlib.h>
    #include <stdio.h>
    #include <iostream>
    #include <string>
    #include <tr1/unordered_map>
    using namespace std;
    using namespace std::tr1;
    class TrieNode {
        public:
            // Initialize your data structure here.
            TrieNode() {
            }
            bool has(char w){
                if(!_adj[w])
                    return false;
                else
                    return true;
            }
            TrieNode* get(char w) {
                if(!has(w))
                    return NULL;
                return _adj[w];
            }
            void add(char w,TrieNode* node) {
                _adj[w]=node;
            }
            bool isTail(){
                return has('#');
            }
            unordered_map<char,TrieNode*> getAdj() {
                return _adj;
            }
        private:
            unordered_map<char,TrieNode*> _adj;
    };
    typedef unordered_map<char,TrieNode*> TrieMap;
    typedef unordered_map<char,TrieNode*>::iterator TrieMapIter;
    class WordDictionary
    {
        public:
            WordDictionary() {
                root = new TrieNode();
            }
    
            // Inserts a word into the trie.
    
            TrieNode* getRoot() {
                return root;
            }
            //For leetcode problem, AddAndSearchWord
            void add(string s,TrieNode* node) {
                if(s.length()==0) {
                    node->add('#',new TrieNode());
                    return;
                }
                char w=s[0];
                if(!node->has(w)) {
                    TrieNode* temp=new TrieNode();
                    node->add(w,temp);
                }
                int len=s.length();
                add(s.substr(1,len-1),node->get(w));
            }
            void addWord(string s) {
                add(s,getRoot());
            }
            bool search(string key,TrieNode* node) {
                int len=key.length();
                if(len==0)
                    return node->isTail();
                char w=key[0];
                TrieNode* next=node;
                string subKey=key.substr(1,len-1);
                if(w=='.') {
                    TrieMap adj=node->getAdj();
                    for(TrieMapIter iter=adj.begin();iter!=adj.end();iter++){
                        next=iter->second;
                        if(next&&search(subKey,next))
                            return true;
                    }
                    return false;
                }
                else {
                    if(!node->has(w))
                        return false;
                    next=node->get(w);
                    return search(subKey,next);
                }
            }
            bool searchWord(string key) {
                if(key.length()==0)
                    return false;
                return search(key,getRoot());
            }
        private:
            TrieNode* root;
    };
    
    // Your Trie object will be instantiated and called as such:
    // Trie trie;
    // trie.insert("somestring");
    // trie.search("key");
    void testSearch(WordDictionary& trie,string s,int result)
    {
        static int cnt=0;
        int ret;
        cout<<"case#"<<cnt++;
        if((ret=trie.searchWord(s))!=result) {
            cout<<"\tWA"<<endl;
            cout<<"input:"<<s<<endl;
            cout<<"expected:\t"<<result<<endl;
            cout<<"output:\t\t"<<ret<<endl;
        }
        else
            cout<<"\tPassed"<<endl;
    
    }
    int main()
    {
        WordDictionary trie;
        trie.addWord("ab");
        trie.addWord("bad");
        trie.addWord("dad");
        trie.addWord("mad");
        testSearch(trie,"bad",true);
        testSearch(trie,"dad",true);
        testSearch(trie,"mad",true);
        testSearch(trie,"pad",false);
    
        testSearch(trie,"b..",true);
        testSearch(trie,"ba.",true);
        testSearch(trie,".a.",true);
        testSearch(trie,".a",false);
        return 0;
    }

Log in to reply
 

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