By Sarthak Deokar
import java.util.*;
class TrieNode {
Map<Character, TrieNode> children;
boolean isEndOfWord;
public TrieNode() {
children = new HashMap<>();
isEndOfWord = false;
}
}
class Trie {
private final TrieNode root;
public Trie() {
root = new TrieNode();
}
// Insert a word into the Trie
public void insert(String word) {
TrieNode current = root;
for (char c : word.toCharArray()) {
current = current.children.computeIfAbsent(c, k -> new TrieNode());
}
current.isEndOfWord = true;
}
// Search for a word in the Trie
public boolean search(String word) {
TrieNode current = root;
for (char c : word.toCharArray()) {
current = current.children.get(c);
if (current == null) {
return false; // Not found
}
}
return current.isEndOfWord; // True if it's a complete word
}
// Check if any word in the Trie starts with the given prefix
public boolean startsWith(String prefix) {
TrieNode current = root;
for (char c : prefix.toCharArray()) {
current = current.children.get(c);
if (current == null) {
return false; // No words with this prefix
}
}
return true; // Found the prefix
}
public static void main(String[] args) {
Trie trie = new Trie();
trie.insert("apple");
trie.insert("app");
// Search for words
System.out.println(trie.search("apple")); // true
System.out.println(trie.search("app")); // true
System.out.println(trie.search("appl")); // false
System.out.println(trie.startsWith("app")); // true
System.out.println(trie.startsWith("ap")); // true
System.out.println(trie.startsWith("a")); // true
System.out.println(trie.startsWith("b")); // false
}
}```