Skip to content

Commit 6ac8457

Browse files
committed
feat: add solutions to lc/lcof2 problems
lc No.0648.Replace Words lcof2 No.063.Replace Words
1 parent d52f1d9 commit 6ac8457

File tree

12 files changed

+747
-7
lines changed

12 files changed

+747
-7
lines changed

README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
208208
### Design
209209

210210
- [LRU Cache](./solution/0100-0199/0146.Lru%20Cache/README_EN.md)
211-
- [Implement Trie (Prefix Tree)](<solution/0200-0299/0208.Implement%20Trie%20(Prefix%20Tree)/README_EN.md>)
211+
- [Implement Trie (Prefix Tree)](./solution/0200-0299/0208.Implement%20Trie%20%28Prefix%20Tree%29/README_EN.md)
212212
- [Implement Trie II (Prefix Tree)](./solution/1800-1899/1804.Implement%20Trie%20II%20%28Prefix%20Tree%29/README_EN.md)
213213
- [Design Circular Queue](./solution/0600-0699/0622.Design%20Circular%20Queue/README_EN.md)
214214
- [Design Circular Deque](./solution/0600-0699/0641.Design%20Circular%20Deque/README_EN.md)

lcof2/剑指 Offer II 063. 替换单词/README.md

+154-2
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,179 @@
6767

6868
<p><meta charset="UTF-8" />注意:本题与主站 648&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/replace-words/">https://leetcode-cn.com/problems/replace-words/</a></p>
6969

70-
7170
## 解法
7271

7372
<!-- 这里可写通用的实现逻辑 -->
7473

74+
前缀树实现。
75+
7576
<!-- tabs:start -->
7677

7778
### **Python3**
7879

7980
<!-- 这里可写当前语言的特殊实现逻辑 -->
8081

8182
```python
82-
83+
class Trie:
84+
def __init__(self) -> None:
85+
self.children = [None] * 26
86+
self.root = None
87+
88+
89+
class Solution:
90+
def replaceWords(self, dictionary: List[str], sentence: str) -> str:
91+
trie = Trie()
92+
for root in dictionary:
93+
cur = trie
94+
for c in root:
95+
idx = ord(c) - ord('a')
96+
if cur.children[idx] is None:
97+
cur.children[idx] = Trie()
98+
cur = cur.children[idx]
99+
cur.root = root
100+
101+
ans = []
102+
for word in sentence.split():
103+
cur = trie
104+
for c in word:
105+
idx = ord(c) - ord('a')
106+
if cur.children[idx] is None or cur.root is not None:
107+
break
108+
cur = cur.children[idx]
109+
ans.append(word if cur.root is None else cur.root)
110+
return ' '.join(ans)
83111
```
84112

85113
### **Java**
86114

87115
<!-- 这里可写当前语言的特殊实现逻辑 -->
88116

89117
```java
118+
class Trie {
119+
Trie[] children = new Trie[26];
120+
String root;
121+
}
122+
123+
class Solution {
124+
public String replaceWords(List<String> dictionary, String sentence) {
125+
Trie trie = new Trie();
126+
for (String root : dictionary) {
127+
Trie cur = trie;
128+
for (char c : root.toCharArray()) {
129+
if (cur.children[c - 'a'] == null) {
130+
cur.children[c - 'a'] = new Trie();
131+
}
132+
cur = cur.children[c - 'a'];
133+
}
134+
cur.root = root;
135+
}
136+
List<String> ans = new ArrayList<>();
137+
for (String word : sentence.split("\\s+")) {
138+
Trie cur = trie;
139+
for (char c : word.toCharArray()) {
140+
if (cur.children[c - 'a'] == null || cur.root != null) {
141+
break;
142+
}
143+
cur = cur.children[c - 'a'];
144+
}
145+
ans.add(cur.root == null ? word : cur.root);
146+
}
147+
return String.join(" ", ans);
148+
}
149+
}
150+
```
151+
152+
### **C++**
153+
154+
```cpp
155+
class Trie {
156+
public:
157+
string root;
158+
vector<Trie*> children;
159+
160+
Trie() {
161+
root = "";
162+
children.resize(26);
163+
}
164+
};
165+
166+
class Solution {
167+
public:
168+
string replaceWords(vector<string>& dictionary, string sentence) {
169+
Trie* trie = new Trie();
170+
for (auto root : dictionary)
171+
{
172+
Trie* cur = trie;
173+
for (char c : root)
174+
{
175+
if (!cur->children[c - 'a']) cur->children[c - 'a'] = new Trie();
176+
cur = cur->children[c - 'a'];
177+
}
178+
cur->root = root;
179+
}
180+
181+
string ans = "";
182+
istringstream is(sentence);
183+
vector<string> ss;
184+
string s;
185+
while (is >> s) ss.push_back(s);
186+
for (auto word : ss)
187+
{
188+
Trie* cur = trie;
189+
for (char c : word)
190+
{
191+
if (!cur->children[c - 'a'] || cur->root != "") break;
192+
cur = cur->children[c - 'a'];
193+
}
194+
ans += cur->root == "" ? word : cur->root;
195+
ans += " ";
196+
}
197+
ans.pop_back();
198+
return ans;
199+
}
200+
};
201+
```
90202

203+
### **Go**
204+
205+
```go
206+
func replaceWords(dictionary []string, sentence string) string {
207+
trie := &Trie{}
208+
for _, root := range dictionary {
209+
cur := trie
210+
for _, c := range root {
211+
c -= 'a'
212+
if cur.children[c] == nil {
213+
cur.children[c] = &Trie{}
214+
}
215+
cur = cur.children[c]
216+
}
217+
cur.root = root
218+
}
219+
220+
var ans []string
221+
for _, word := range strings.Split(sentence, " ") {
222+
cur := trie
223+
for _, c := range word {
224+
c -= 'a'
225+
if cur.children[c] == nil || cur.root != "" {
226+
break
227+
}
228+
cur = cur.children[c]
229+
}
230+
if cur.root == "" {
231+
ans = append(ans, word)
232+
} else {
233+
ans = append(ans, cur.root)
234+
}
235+
}
236+
return strings.Join(ans, " ")
237+
}
238+
239+
type Trie struct {
240+
children [26]*Trie
241+
root string
242+
}
91243
```
92244

93245
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Trie {
2+
public:
3+
string root;
4+
vector<Trie*> children;
5+
6+
Trie() {
7+
root = "";
8+
children.resize(26);
9+
}
10+
};
11+
12+
class Solution {
13+
public:
14+
string replaceWords(vector<string>& dictionary, string sentence) {
15+
Trie* trie = new Trie();
16+
for (auto root : dictionary)
17+
{
18+
Trie* cur = trie;
19+
for (char c : root)
20+
{
21+
if (!cur->children[c - 'a']) cur->children[c - 'a'] = new Trie();
22+
cur = cur->children[c - 'a'];
23+
}
24+
cur->root = root;
25+
}
26+
27+
string ans = "";
28+
istringstream is(sentence);
29+
vector<string> ss;
30+
string s;
31+
while (is >> s) ss.push_back(s);
32+
for (auto word : ss)
33+
{
34+
Trie* cur = trie;
35+
for (char c : word)
36+
{
37+
if (!cur->children[c - 'a'] || cur->root != "") break;
38+
cur = cur->children[c - 'a'];
39+
}
40+
ans += cur->root == "" ? word : cur->root;
41+
ans += " ";
42+
}
43+
ans.pop_back();
44+
return ans;
45+
}
46+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
func replaceWords(dictionary []string, sentence string) string {
2+
trie := &Trie{}
3+
for _, root := range dictionary {
4+
cur := trie
5+
for _, c := range root {
6+
c -= 'a'
7+
if cur.children[c] == nil {
8+
cur.children[c] = &Trie{}
9+
}
10+
cur = cur.children[c]
11+
}
12+
cur.root = root
13+
}
14+
15+
var ans []string
16+
for _, word := range strings.Split(sentence, " ") {
17+
cur := trie
18+
for _, c := range word {
19+
c -= 'a'
20+
if cur.children[c] == nil || cur.root != "" {
21+
break
22+
}
23+
cur = cur.children[c]
24+
}
25+
if cur.root == "" {
26+
ans = append(ans, word)
27+
} else {
28+
ans = append(ans, cur.root)
29+
}
30+
}
31+
return strings.Join(ans, " ")
32+
}
33+
34+
type Trie struct {
35+
children [26]*Trie
36+
root string
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Trie {
2+
Trie[] children = new Trie[26];
3+
String root;
4+
}
5+
6+
class Solution {
7+
public String replaceWords(List<String> dictionary, String sentence) {
8+
Trie trie = new Trie();
9+
for (String root : dictionary) {
10+
Trie cur = trie;
11+
for (char c : root.toCharArray()) {
12+
if (cur.children[c - 'a'] == null) {
13+
cur.children[c - 'a'] = new Trie();
14+
}
15+
cur = cur.children[c - 'a'];
16+
}
17+
cur.root = root;
18+
}
19+
List<String> ans = new ArrayList<>();
20+
for (String word : sentence.split("\\s+")) {
21+
Trie cur = trie;
22+
for (char c : word.toCharArray()) {
23+
if (cur.children[c - 'a'] == null || cur.root != null) {
24+
break;
25+
}
26+
cur = cur.children[c - 'a'];
27+
}
28+
ans.add(cur.root == null ? word : cur.root);
29+
}
30+
return String.join(" ", ans);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Trie:
2+
def __init__(self) -> None:
3+
self.children = [None] * 26
4+
self.root = None
5+
6+
7+
class Solution:
8+
def replaceWords(self, dictionary: List[str], sentence: str) -> str:
9+
trie = Trie()
10+
for root in dictionary:
11+
cur = trie
12+
for c in root:
13+
idx = ord(c) - ord('a')
14+
if cur.children[idx] is None:
15+
cur.children[idx] = Trie()
16+
cur = cur.children[idx]
17+
cur.root = root
18+
19+
ans = []
20+
for word in sentence.split():
21+
cur = trie
22+
for c in word:
23+
idx = ord(c) - ord('a')
24+
if cur.children[idx] is None or cur.root is not None:
25+
break
26+
cur = cur.children[idx]
27+
ans.append(word if cur.root is None else cur.root)
28+
return ' '.join(ans)

0 commit comments

Comments
 (0)