RunTimeError for TestCase search("a")


  • 2
    V
    class TrieNode {
        // Initialize your data structure here.
        
        private static final int SIZE = 26;
        private boolean isEnd;
        public TrieNode() {
            children = new TrieNode[26];
        }
        public void setEnd(boolean end){
            isEnd = end;
        }
        public boolean getEnd(){
            return isEnd;
        }
        private TrieNode[] children;
        
        public  boolean doesIndexExist(int x){
            
            if(x < 0 || x>25)
            
             throw new IllegalArgumentException("Bad Input/Bug in program");
             
             return(children[x] == null);
        }
        
        public  TrieNode createIndex(int x){
            
            TrieNode child = null;
            
            if(!doesIndexExist(x)){
                children[x] = new TrieNode();
                return children[x];
            }
            
           return children[x];
        }
        
    }
    
    public class Trie {
        private TrieNode root;
    
        public Trie() {
            root = new TrieNode();
        }
    
        // Inserts a word into the trie.
        public void insert(String word) {
            
          TrieNode curr = root;
          
          char[] words = word.toCharArray();
          
          for(char ch :words){
              
            int index = ch-'a';
             
            curr = curr.createIndex(index);
            
            
            
           }
           curr.setEnd(true);
            
        }
    
        // Returns if the word is in the trie.
        public boolean search(String word) {
            
            if(null == word)
             
              return false;
              
            if(word.trim().length() == 0)
             
             return false;
             
             char[] words = word.toCharArray();
             
             TrieNode curr = root;
             
             
             for(char ch : words) {
                 
               int index = ch-'a';
               
               if(!curr.doesIndexExist(index)){
                   return false;
                 }
             
              curr = curr.createIndex(index);
                 
             }
             
             return(curr.getEnd());
             
         }
    
        // Returns if there is any word in the trie
        // that starts with the given prefix.
        public boolean startsWith(String word) {
            
             if(null == word)
             
              return false;
              
            if(word.trim().length() == 0)
             
             return false;
             
             char[] words = word.toCharArray();
             
             TrieNode curr = root;
             
             
             for(char ch : words) {
                 
               int index = ch-'a';
               
               if(!curr.doesIndexExist(index)){
                   return false;
                 }
             
              curr = curr.createIndex(index);
                 
             }
             
             return(true);
             
            
        }
    }

Log in to reply
 

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