-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution1.java
48 lines (45 loc) · 1.38 KB
/
Solution1.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
//HashSet solution
//Justin Butler
//11/12/2021
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;} // improve search functionality for single chars
dict.add(word);
}
public boolean search(String word) {
if(word.contains("."))
{
if(word.equals(".")) // edge case that slows us down
{return singleChar;}
char[] arr = word.toCharArray();
for(String i : dict) // search for match
{
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);
}
}