-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReplaceWords.java
110 lines (96 loc) · 3.05 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*https://leetcode.com/problems/replace-words/*/
class TrieNode
{
TrieNode[] hash;
boolean isEnd;
TrieNode()
{
hash = new TrieNode[26];
isEnd = false;
}
}
class Solution {
public String replaceWords(List<String> dictionary, String sentence) {
TrieNode trie = new TrieNode();
for (String word : dictionary)
{
TrieNode temp = trie;
for (char ch : word.toCharArray())
{
int index = ch-'a';
if (temp.hash[index] == null)
temp.hash[index] = new TrieNode();
temp = temp.hash[index];
}
temp.isEnd = true;
}
StringBuilder build = new StringBuilder(), finalString = new StringBuilder();
int index = 0, n = sentence.length();
String[] sentenceWords = sentence.split(" +");
for (int i = 0; i < sentenceWords.length; ++i)
{
if (i != 0) finalString.append(" ");
String word = sentenceWords[i];
StringBuilder root = new StringBuilder();
TrieNode temp = trie;
for (char ch : word.toCharArray())
{
if (temp.hash[ch-'a'] == null || temp.isEnd) break;
temp = temp.hash[ch-'a'];
root.append(ch);
}
if (temp.isEnd) finalString.append(root);
else finalString.append(word);
}
return finalString.toString();
}
}
class Solution {
class TrieNode {
boolean isEnd;
TrieNode[] children;
public TrieNode() {
children = new TrieNode[26];
}
}
TrieNode root;
public void insert(String word) {
TrieNode curr = root;
for(int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if(curr.children[c-'a'] == null) {
curr.children[c-'a'] = new TrieNode();
}
curr = curr.children[c-'a'];
}
curr.isEnd = true;
}
public String replaceWords(List<String> dictionary, String sentence) {
root = new TrieNode();
for(String s : dictionary) {
insert(s);
}
StringBuilder res = new StringBuilder();
String[] all = sentence.split(" ");
for(int k = 0; k < all.length; k++) {
String word = all[k];
StringBuilder replacement = new StringBuilder();
TrieNode curr = root;
if(k != 0) res.append(" ");
for(int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if(curr.children[c-'a'] == null || curr.isEnd) {
break;
}
replacement.append(c);
curr = curr.children[c-'a'];
}
if(curr.isEnd) {
res.append(replacement);
} else {
res.append(word);
}
}
return res.toString();
}
}