Even shorter code combine them all in one


  • 1
    A

    Just for fun, from the perspective of OOD you maybe don't want to do this.

    class TrieNode {
        public bool isWord;
        public TrieNode[] children;
        // Initialize your data structure here.
        public TrieNode() {
            isWord = false;
            children = new TrieNode[26];
        }
    }
    
    public class Trie {
        private TrieNode root;
    
        public Trie() {
            root = new TrieNode();
        }
    
        // Inserts a word into the trie.
        public void Insert(String word) {
            insertOrSearch(word, true, false);
        }
    
        // Returns if the word is in the trie.
        public bool Search(string word) {
            return insertOrSearch(word, false, true);
        }
    
        // Returns if there is any word in the trie
        // that starts with the given prefix.
        public bool StartsWith(string word) {
            return insertOrSearch(word, false, false);
        }
        
        private bool insertOrSearch(string word, bool isInsert, bool searchWord){
            TrieNode cur = root;
            for (int i=0; i<word.Length; i++){
                int index = word[i]-'a';
                if (cur.children[index]==null)
                { 
                    if (isInsert){ cur.children[index] = new TrieNode(); }
                    else { return false; }
                }
                cur = cur.children[index];
            }
            if (isInsert) { cur.isWord = true; }
            return searchWord?cur.isWord:true;
        }
    }

  • 0
    X

    too many error of you code

    class TrieNode {
    // Initialize your data structure here.
    public boolean isWord; 
    public TrieNode[] children = new TrieNode[26];
    
    public TrieNode() {
        
    }
    

    }

    public class Trie {
    private TrieNode root;

    public Trie() {
        root = new TrieNode();
    }
    
     // Inserts a word into the trie.
    public void insert(String word) {
        insertOrSearch(word, true, false);
    }
    
    // Returns if the word is in the trie.
    public boolean search(String word) {
        return insertOrSearch(word, false, true);
    }
    
    // Returns if there is any word in the trie
    // that starts with the given prefix.
    public boolean startsWith(String word) {
        return insertOrSearch(word, false, false);
    }
    
    private boolean insertOrSearch(String word, boolean isInsert, boolean searchWord){
        TrieNode cur = root;
        for (int i=0; i<word.length(); i++){
            int index = word.charAt(i)-'a';
            if (cur.children[index]==null){ 
                if (isInsert){ cur.children[index] = new TrieNode(); }
                else { return false; }
            }
            cur = cur.children[index];
        }
        if (isInsert) { cur.isWord = true; }
        return searchWord?cur.isWord:true;
    }
    

    }


Log in to reply
 

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