diff --git a/java/208-Implement-Trie(Prefix Tree) b/java/208-Implement-Trie(Prefix Tree) new file mode 100644 index 000000000..1f68fb540 --- /dev/null +++ b/java/208-Implement-Trie(Prefix Tree) @@ -0,0 +1,54 @@ +class Trie { + Node root; + + public Trie() { + root = new Node('\0'); //dummy node + } + + public void insert(String word) { + Node curr = root; + for (char x : word.toCharArray()) { + if (curr.children[x - 'a'] == null) { + curr.children[x - 'a'] = new Node(x); + } + curr = curr.children[x - 'a']; + } + curr.isWord = true; + } + + public boolean search(String word) { + Node res = getLast(word); + return (res != null && res.isWord); + } + + //helper method + public Node getLast(String word) { + Node curr = root; + for (char x : word.toCharArray()) { + if (curr.children[x - 'a'] == null) { + return null; + } + + curr = curr.children[x - 'a']; + } + return curr; + } + + public boolean startsWith(String prefix) { + Node res = getLast(prefix); + if (res == null) return false; + return true; + } + + class Node { + private char value; + private boolean isWord; + private Node[] children; + + public Node(char val) { + this.value = val; + this.isWord = false; + this.children = new Node[26]; + } + } +}