/
ReplaceWords.java
57 lines (45 loc) · 1.61 KB
/
ReplaceWords.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
// https://leetcode.com/problems/replace-words
class Solution {
public String replaceWords(List<String> dictionary, String sentence) {
TrieNode head = new TrieNode(), t = head;
// Build a trie from dictionay
for (String word: dictionary) {
for (char c: word.toCharArray()) {
if (t.children[c - 'a'] == null) {
t.children[c - 'a'] = new TrieNode();
}
t = t.children[c - 'a'];
}
t.word = word;
t = head;
}
List<String> res = new ArrayList<>();
// Now for each word in sentence, see if there is a matching prefix in dictionary
for (String word: sentence.split(" ")) {
boolean hasMatch = false;
for (char c: word.toCharArray()) {
if (t.word != null) {
// We have a match
hasMatch = true;
res.add(t.word);
break;
}
if (t.children[c - 'a'] == null) {
// We dont have a match
break;
}
t = t.children[c - 'a'];
}
if (!hasMatch) {
res.add(word);
}
t = head;
}
return String.join(" ", res);
}
class TrieNode {
TrieNode[] children = new TrieNode[26];
// Word denoted by this terminal
String word;
}
}