Untitled

 avatar
Darin
plain_text
a year ago
788 B
4
Indexable
Never
class Trie {
    private static final int ALPHABET_SIZE = 26;
    private final Trie[] node = new Trie[ALPHABET_SIZE];
    private boolean isEnd;

    public void insert(String word) {
        Trie temp = this;
        for (char c : word.toCharArray()) {
            int index = c - 'a';
            if (temp.node[index] == null) {
                temp.node[index] = new Trie();
            }
            temp = temp.node[index];
        }
        temp.isEnd = true;
    }

    public boolean search(String word) {
        Trie temp = this;
        for (char c : word.toCharArray()) {
            int index = c - 'a';
            if (temp.node[index] == null) {
                return false;
            }
            temp = temp.node[index];
        }
        return temp.isEnd;
    }
}