-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path43.DesignSearchSuggestionORAUTOCOMPLETE.java
More file actions
93 lines (74 loc) · 3.62 KB
/
43.DesignSearchSuggestionORAUTOCOMPLETE.java
File metadata and controls
93 lines (74 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.*;
// ------------------------------------------------------------
// AUTOCOMPLETE LLD - JAVA (Single File) with Line-by-Line EXPL
// ------------------------------------------------------------
public class AutoComplete {
// --------------------------
// Trie Node Definition
// --------------------------
static class TrieNode {
Map<Character, TrieNode> children; // Each child node for characters
boolean isEnd; // Marks end of a valid word
TrieNode() { // Constructor
children = new HashMap<>(); // Initialize child map
isEnd = false; // Initially not an end node
}
}
// ----------------------------------------
// AutoCompleteSystem using Trie (Facade)
// ----------------------------------------
static class AutoCompleteSystem {
private TrieNode root; // Root of Trie
AutoCompleteSystem() { // Constructor
root = new TrieNode(); // Initialize root node
}
// Insert a word into the Trie
public void insert(String word) {
TrieNode node = root; // Start from root
for (char ch : word.toCharArray()) { // Iterate each character
if (!node.children.containsKey(ch)) { // If child missing
node.children.put(ch, new TrieNode()); // Create new node
}
node = node.children.get(ch); // Move to child node
}
node.isEnd = true; // Mark completion of the word
}
// Collect all words after a given prefix node (DFS)
private void collect(TrieNode node, String prefix, List<String> result) {
if (node.isEnd) { // If valid word ends here
result.add(prefix); // Add to result list
}
List<Character> keys = new ArrayList<>(node.children.keySet());
Collections.sort(keys); // Sort keys for lexicographic output
for (char ch : keys) { // DFS into each child
collect(node.children.get(ch), prefix + ch, result);
}
}
// Suggest words based on prefix
public List<String> suggest(String prefix) {
TrieNode node = root; // Start from root
for (char ch : prefix.toCharArray()) { // Traverse Trie
if (!node.children.containsKey(ch)) { // Prefix not found
return new ArrayList<>(); // Empty list
}
node = node.children.get(ch); // Move deeper
}
List<String> result = new ArrayList<>(); // Store suggestions
collect(node, prefix, result); // DFS to collect all words
return result; // Return results
}
}
// ---------------------------------------
// MAIN METHOD — RUN DEMO
// ---------------------------------------
public static void main(String[] args) {
AutoCompleteSystem ac = new AutoCompleteSystem(); // Create system
// Sample dataset of words
String[] words = {"apple", "app", "ape", "bat", "ball", "banana"};
for (String w : words) ac.insert(w); // Insert into Trie
// Print demo outputs
System.out.println("Suggestions for 'ap' : " + ac.suggest("ap"));
System.out.println("Suggestions for 'ba' : " + ac.suggest("ba"));
System.out.println("Suggestions for 'app': " + ac.suggest("app"));
}
}