208. Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods.

class TrieNode {
    // Initialize your data structure here.
    Map<Character, TrieNode> children = new HashMap<>();
    public boolean hasWord = false;
    public TrieNode() {

    }

    public void insert(String word, int k){
        if(k == word.length()){
            hasWord = true;
            return;
        }
        Character c = word.charAt(k);
        if(!children.containsKey(c)){
            children.put(c, new TrieNode());
        }
        ((TrieNode)children.get(c)).insert(word, k+1);
    }
    public TrieNode find(String word, int k){
        if(k == word.length()){
            return this;
        }
        Character c = word.charAt(k);
        TrieNode tn = (TrieNode)children.get(c);
        if(tn == null) return null;
        else return tn.find(word, k+1);
    }
}

public class Trie {
    private TrieNode root;

    public Trie() {
        root = new TrieNode();
    }

    // Inserts a word into the trie.
    public void insert(String word) {
        root.insert(word, 0);
    }

    // Returns if the word is in the trie.
    public boolean search(String word) {
        TrieNode tn = root.find(word, 0);
        return tn != null && tn.hasWord;
    }

    // Returns if there is any word in the trie
    // that starts with the given prefix.
    public boolean startsWith(String prefix) {
        return root.find(prefix, 0) != null;
    }
}

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

A clear/ better solution

class TrieNode {
    // Initialize your data structure here.
    TrieNode[] children = new TrieNode[26];
    String word = "";
    public TrieNode() {

    }
}

public class Trie {
    private TrieNode root;

    public Trie() {
        root = new TrieNode();
    }

    // Inserts a word into the trie.
    public void insert(String word) {
        TrieNode node = root;
        for(char ch : word.toCharArray()){
            if(node.children[ch - 'a'] == null){
                node.children[ch-'a'] = new TrieNode();
            }   
            node = node.children[ch - 'a'];
        }

        node.word = word;
    }

    // Returns if the word is in the trie.
    public boolean search(String word) {
        TrieNode node = root;
        for(char ch : word.toCharArray()){
            if(node.children[ch - 'a'] == null) return false;
            node = node.children[ch - 'a'];
        }
        return node.word.equals(word);
    }

    // Returns if there is any word in the trie
    // that starts with the given prefix.
    public boolean startsWith(String prefix) {
        TrieNode node = root;
        for(char ch : prefix.toCharArray()){
            if(node.children[ch - 'a'] == null) return false;
            node = node.children[ch - 'a'];
        }
        return true;
    }
}

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

results matching ""

    No results matching ""