Share my code and ... is this test case error?


  • 0
    R
    public class TireTree{
        public char chr;
        public List<TireTree> node = new List<TireTree>();
        
        
        public TireTree(char _chr)
        {
            chr = _chr;
        }
    }
    

    public class WordDictionary {

    TireTree root = new TireTree(' ');
    // Adds a word into the data structure.
    public void AddWord(String word) {
        char[] chr = word.ToCharArray();
        TireTree temp = root;
        
        for(int i=0; i< chr.Length; i++)
        {
            int j = 0;
            for(; j<temp.node.Count; j++)
            {
               if(temp.node[j].chr== chr[i])
               {
                   temp = temp.node[j];
                   break;
               }
            }
            
            // can't find this one, add a new one
            if(j == temp.node.Count || j == 0){
                TireTree t = new TireTree(chr[i]);
                temp.node.Add(t);
                temp = t;
            }
        }
    }
    
    // Returns if the word is in the data structure. A word could
    // contain the dot character '.' to represent any one letter.
    public bool Search(string word) {
        bool result = false;
        
        foreach(TireTree node in root.node)
        {
            result |= SearchHelper(node, word);
        }
        
        return result;
    }
    
    bool SearchHelper(TireTree root, string word)
    {
        if(root == null && (word ==null || word.Length == 0)) return true;
        if(root == null) return false;
        if(word == null || word.Count() == 0) return false;
        if(word.Count() == 1 && root.node.Count() == 0)
        {
            if(word.ToCharArray()[0] == '.') return true;
            if(word.ToCharArray()[0] == root.chr) return true;
            return false;
        }
        
        if(word.ToCharArray()[0] != '.' && word.ToCharArray()[0] != root.chr) return false;
        foreach(TireTree node in root.node)
        {
            if(SearchHelper(node, word.Substring(1, word.Count() -1)))
                return true;
        }
        
        return false;
    }
    

    }

    The test cases:
    Input:
    addWord("a"),addWord("a"),search("."),search("a"),search("aa"),search("a"),search(".a"),search("a.")
    Output:
    [false,false,true,false,true,true]
    Expected:
    [true,true,false,true,false,false]


Log in to reply
 

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