Accept Java Solution


  • 0
    D

    TrieNode
    {
    // Initialize your data structure here.
    TrieNode[] children;
    boolean exist;
    public TrieNode() {
    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) {
        TrieNode p = root;
        for (int i = 0; i < word.length(); i++) {
            int ind = word.charAt(i) - 'a';
            if (p.children[ind] == null) {
                p.children[ind] = new TrieNode();
            }
            p = p.children[ind];
        }
        p.exist = true;
    }
    
    // Returns if the word is in the trie.
    public boolean search(String word) {
        TrieNode p = root;
        for (int i = 0; i < word.length(); i++) {
            int ind = word.charAt(i) - 'a';
            if (p.children[ind] == null) {
                return false;
            }
            p = p.children[ind];
        }
        return p.exist;
    }
    
    // Returns if there is any word in the trie
    // that starts with the given prefix.
    public boolean startsWith(String prefix) {
        TrieNode p = root;
        for (int i = 0; i < prefix.length(); i++) {
            int ind = prefix.charAt(i) - 'a';
            if (p.children[ind] == null) {
                return false;
            }
            p = p.children[ind];
        }
        return true;
    }
    

    }

    // Your Trie object will be instantiated and called as such:
    // Trie trie = new Trie();
    // trie.insert("somestring");
    // trie.search("key");


  • 0
    B

    Is it correct?

    Trie trie = new Trie();
    trie.insert("a");
    trie.insert("ab");
    trie.search("a"); // what's the output?
    

Log in to reply
 

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