-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution2.Java
More file actions
62 lines (57 loc) · 1.54 KB
/
Solution2.Java
File metadata and controls
62 lines (57 loc) · 1.54 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
//Justin Butler
//11/12/2021
/*
Runtime: 42 ms
Memory Usage: 51 MB
*/
class WordDictionary {
HashMap<Integer, HashSet<String>> hm;
public WordDictionary() {
hm = new HashMap<Integer, HashSet<String>>();
}
public void addWord(String word) {
int size = word.length();
if(hm.containsKey(size))
{
hm.get(size).add(word);
}
else
{
hm.put(size, new HashSet<String>());
hm.get(size).add(word);
}
}
public boolean search(String word) {
int size = word.length();
if(hm.containsKey(size))
{
if(word.equals(".")){return true;}
if(word.contains("."))
{
HashSet<String> curr = hm.get(size);
for(String i : curr)
{
StringBuilder bld = new StringBuilder();
for(int j = 0; j < i.length(); j++)
{
if(word.charAt(j)=='.')
{
bld.append('.');
}
else
{
bld.append(i.charAt(j));
}
}
if(word.equals(bld.toString())){return true;}
}
return false;
}
else
{
return(hm.get(size).contains(word));
}
}
return false;
}
}