-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDesignAddAndSearchWordsDataStructure.java
44 lines (42 loc) · 1.2 KB
/
DesignAddAndSearchWordsDataStructure.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
class WordDictionary {
HashSet<String> dict;
boolean singleChar = false;
public WordDictionary() {
dict = new HashSet<String>();
}
public void addWord(String word) {
if(word.length()==1){singleChar = true;}
dict.add(word);
}
public boolean search(String word) {
if(word.contains("."))
{
if(word.equals("."))
{return singleChar;}
char[] arr = word.toCharArray();
for(String i : dict)
{
if(i.length() == arr.length)
{
StringBuilder curr = new StringBuilder();
for(int j = 0; j < arr.length; j++)
{
if(arr[j]=='.')
{
curr.append('.');
}
else
{
curr.append(i.charAt(j));
}
}
if(curr.toString().equals(word))
{
return true;
}
}
}
}
return dict.contains(word);
}
}