-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0211_Design_Add_and_Search_Words_Data_Structure.java
106 lines (89 loc) · 3.34 KB
/
0211_Design_Add_and_Search_Words_Data_Structure.java
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
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* 211. Design Add and Search Words Data Structure
* Problem Link: https://leetcode.com/problems/design-add-and-search-words-data-structure/
* Difficulty: Medium
*
* Solution Created by: Muhammad Khuzaima Umair
* LeetCode : https://leetcode.com/mkhuzaima/
* Github : https://github.com/mkhuzaima
* LinkedIn : https://www.linkedin.com/in/mkhuzaima/
*/
class WordDictionary {
TreeNode root;
// Constructor to initialize the root node of the trie
public WordDictionary() {
root = new TreeNode();
}
// Method to add a new word to the trie
public void addWord(String word) {
TreeNode node = root;
// Traverse the trie to add each character of the word
for (int i = 0; i < word.length(); i++) {
node = node.putIfAbsent(word.charAt(i));
}
// Mark the last node as a word
node.setWord();
}
// Method to search for a word in the trie
public boolean search(String word) {
// Start searching from the root node
return root.search(word, 0);
}
private class TreeNode {
// Define the number of letters in the alphabet
private static int BRANCHING_FACTOR = 26;
// Array to store the children of the node
TreeNode[] children;
// Flag to indicate if the node represents a complete word
private boolean isWord;
// Constructor to initialize a new node
public TreeNode() {
children = new TreeNode[BRANCHING_FACTOR];
isWord = false;
}
// Getter method to check if the node represents a complete word
public boolean isWord() {
return isWord;
}
// Setter method to mark a node as a complete word
public void setWord () {
isWord = true;
}
// Method to get the child node for a given character
public TreeNode get(char c) {
return children[c -'a'];
}
// Method to add a child node for a given character, if it doesn't exist
public TreeNode putIfAbsent(char c) {
c -= 'a'; // Convert the letter from 0 to 25
if (children[c] == null) {
children[c] = new TreeNode();
}
return children[c];
}
// Method to search for a word in the trie
public boolean search(String word, int index) {
// If we've reached the end of the word, check if the node represents a complete word
if (index >= word.length()) return isWord;
// Start searching for the next character from this node
TreeNode node = root;
// If the next character is a '.', search among each child recursively
if (word.charAt(index) == '.') {
for (TreeNode child: children) {
if (child != null && child.search(word, index + 1) ) {
return true;
}
}
}
// If the next character is not a '.', search for only the given child
else {
TreeNode child = get(word.charAt(index));
if (child != null && child.search(word, index + 1)) {
return true;
}
}
// The word was not found in the trie
return false;
}
}
}