-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm648 v2 TRIE.java
65 lines (55 loc) · 2.03 KB
/
m648 v2 TRIE.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
// This could be better optimized by just having an array of 26 elements instead of a hashmap
// HashMap creation results in major cost time-wise and complexity (space) wise
// Since it's mapped to 26 lowercase chars, it's simpler that way since the spots will just be null
// when undeclared /shrug
class Solution {
public String replaceWords(List<String> dictionary, String sentence) {
helper trie = new helper();
for (String s : dictionary) {
helper currentNode = trie;
for (Character c : s.toCharArray()) {
if (currentNode.end) { // unnecessary to continue since exists a prefix that's shorter
break;
}
if (currentNode.vals.containsKey(c)) {
currentNode = currentNode.vals.get(c);
} else {
currentNode = currentNode.addCase(c);
}
}
currentNode.end = true;
}
StringBuilder output = new StringBuilder();
for (String word : sentence.split(" ")) {
helper currentNode = trie;
for (Character c : word.toCharArray()) {
output.append(c);
if (currentNode == null) {
continue;
}
if (currentNode.end) {
output.deleteCharAt(output.length() - 1);
break;
}
currentNode = currentNode.vals.getOrDefault(c, null);
}
output.append(" ");
}
output.deleteCharAt(output.length() - 1);
return output.toString();
}
class helper {
private HashMap<Character, helper> vals;
private boolean end = false;
public helper() {
this.vals = new HashMap<>();
}
public helper addCase(Character c) {
if (end) {
return null;
}
vals.put(c, new helper());
return vals.get(c);
}
}
}