Skip to content

Commit 8b172ed

Browse files
committed
feat: update solutions to lc problem: No.0648
No.0648.Replace Words
1 parent ff4082e commit 8b172ed

File tree

8 files changed

+54
-38
lines changed

8 files changed

+54
-38
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
- [划分为 k 个相等的子集](/solution/0600-0699/0698.Partition%20to%20K%20Equal%20Sum%20Subsets/README.md) - `DFS``回溯``剪枝`
9898
- [完成所有工作的最短时间](/solution/1700-1799/1723.Find%20Minimum%20Time%20to%20Finish%20All%20Jobs/README.md) - `DFS``回溯``剪枝`
9999
- [公平分发饼干](/solution/2300-2399/2305.Fair%20Distribution%20of%20Cookies/README.md) - `DFS``回溯``剪枝`
100+
- [矩阵中的最长递增路径](/solution/0300-0399/0329.Longest%20Increasing%20Path%20in%20a%20Matrix/README.md) - `DFS``记忆化搜索`
101+
- [网格图中递增路径的数目](/solution/2300-2399/2328.Number%20of%20Increasing%20Paths%20in%20a%20Grid/README.md) - `DFS``记忆化搜索`
100102

101103
<!-- DFS 待补充 -->
102104

README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
9595
- [Partition to K Equal Sum Subsets](/solution/0600-0699/0698.Partition%20to%20K%20Equal%20Sum%20Subsets/README_EN.md) - `DFS`, `Backtracking`
9696
- [Find Minimum Time to Finish All Jobs](/solution/1700-1799/1723.Find%20Minimum%20Time%20to%20Finish%20All%20Jobs/README_EN.md) - `DFS`, `Backtracking`
9797
- [Fair Distribution of Cookies](/solution/2300-2399/2305.Fair%20Distribution%20of%20Cookies/README_EN.md) - `DFS`, `Backtracking`
98+
- [Longest Increasing Path in a Matrix](/solution/0300-0399/0329.Longest%20Increasing%20Path%20in%20a%20Matrix/README_EN.md) - `DFS``Memoization`
99+
- [Number of Increasing Paths in a Grid](/solution/2300-2399/2328.Number%20of%20Increasing%20Paths%20in%20a%20Grid/README.md) - `DFS``Memoization`
98100

99101
### 4. Dynamic Programming(DP)
100102

solution/0300-0399/0329.Longest Increasing Path in a Matrix/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
记忆化搜索。
52+
**方法一:记忆化搜索**
53+
54+
时间复杂度 $O(mn)$。
55+
56+
相似题目:[2328. 网格图中递增路径的数目](/solution/2300-2399/2328.Number%20of%20Increasing%20Paths%20in%20a%20Grid/README.md)
5357

5458
<!-- tabs:start -->
5559

solution/0600-0699/0648.Replace Words/README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53-
前缀树实现。
53+
**方法一:前缀树**
5454

5555
<!-- tabs:start -->
5656

@@ -60,7 +60,7 @@
6060

6161
```python
6262
class Trie:
63-
def __init__(self) -> None:
63+
def __init__(self):
6464
self.children = [None] * 26
6565
self.root = None
6666

@@ -71,7 +71,7 @@ class Solution:
7171
for root in dictionary:
7272
cur = trie
7373
for c in root:
74-
idx = ord(c) - ord('a')
74+
idx = ord(c) - ord("a")
7575
if cur.children[idx] is None:
7676
cur.children[idx] = Trie()
7777
cur = cur.children[idx]
@@ -81,12 +81,12 @@ class Solution:
8181
for word in sentence.split():
8282
cur = trie
8383
for c in word:
84-
idx = ord(c) - ord('a')
85-
if cur.children[idx] is None or cur.root is not None:
84+
idx = ord(c) - ord("a")
85+
if cur.children[idx] is None or cur.root:
8686
break
8787
cur = cur.children[idx]
88-
ans.append(word if cur.root is None else cur.root)
89-
return ' '.join(ans)
88+
ans.append(cur.root or word)
89+
return " ".join(ans)
9090
```
9191

9292
### **Java**
@@ -105,21 +105,23 @@ class Solution {
105105
for (String root : dictionary) {
106106
Trie cur = trie;
107107
for (char c : root.toCharArray()) {
108-
if (cur.children[c - 'a'] == null) {
109-
cur.children[c - 'a'] = new Trie();
108+
c -= 'a';
109+
if (cur.children[c] == null) {
110+
cur.children[c] = new Trie();
110111
}
111-
cur = cur.children[c - 'a'];
112+
cur = cur.children[c];
112113
}
113114
cur.root = root;
114115
}
115116
List<String> ans = new ArrayList<>();
116-
for (String word : sentence.split("\\s+")) {
117+
for (String word : sentence.split(" ")) {
117118
Trie cur = trie;
118119
for (char c : word.toCharArray()) {
119-
if (cur.children[c - 'a'] == null || cur.root != null) {
120+
c -= 'a';
121+
if (cur.children[c] == null || cur.root != null) {
120122
break;
121123
}
122-
cur = cur.children[c - 'a'];
124+
cur = cur.children[c];
123125
}
124126
ans.add(cur.root == null ? word : cur.root);
125127
}

solution/0600-0699/0648.Replace Words/README_EN.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
```python
5050
class Trie:
51-
def __init__(self) -> None:
51+
def __init__(self):
5252
self.children = [None] * 26
5353
self.root = None
5454

@@ -59,7 +59,7 @@ class Solution:
5959
for root in dictionary:
6060
cur = trie
6161
for c in root:
62-
idx = ord(c) - ord('a')
62+
idx = ord(c) - ord("a")
6363
if cur.children[idx] is None:
6464
cur.children[idx] = Trie()
6565
cur = cur.children[idx]
@@ -69,12 +69,12 @@ class Solution:
6969
for word in sentence.split():
7070
cur = trie
7171
for c in word:
72-
idx = ord(c) - ord('a')
73-
if cur.children[idx] is None or cur.root is not None:
72+
idx = ord(c) - ord("a")
73+
if cur.children[idx] is None or cur.root:
7474
break
7575
cur = cur.children[idx]
76-
ans.append(word if cur.root is None else cur.root)
77-
return ' '.join(ans)
76+
ans.append(cur.root or word)
77+
return " ".join(ans)
7878
```
7979

8080
### **Java**
@@ -91,21 +91,23 @@ class Solution {
9191
for (String root : dictionary) {
9292
Trie cur = trie;
9393
for (char c : root.toCharArray()) {
94-
if (cur.children[c - 'a'] == null) {
95-
cur.children[c - 'a'] = new Trie();
94+
c -= 'a';
95+
if (cur.children[c] == null) {
96+
cur.children[c] = new Trie();
9697
}
97-
cur = cur.children[c - 'a'];
98+
cur = cur.children[c];
9899
}
99100
cur.root = root;
100101
}
101102
List<String> ans = new ArrayList<>();
102-
for (String word : sentence.split("\\s+")) {
103+
for (String word : sentence.split(" ")) {
103104
Trie cur = trie;
104105
for (char c : word.toCharArray()) {
105-
if (cur.children[c - 'a'] == null || cur.root != null) {
106+
c -= 'a';
107+
if (cur.children[c] == null || cur.root != null) {
106108
break;
107109
}
108-
cur = cur.children[c - 'a'];
110+
cur = cur.children[c];
109111
}
110112
ans.add(cur.root == null ? word : cur.root);
111113
}

solution/0600-0699/0648.Replace Words/Solution.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@ public String replaceWords(List<String> dictionary, String sentence) {
99
for (String root : dictionary) {
1010
Trie cur = trie;
1111
for (char c : root.toCharArray()) {
12-
if (cur.children[c - 'a'] == null) {
13-
cur.children[c - 'a'] = new Trie();
12+
c -= 'a';
13+
if (cur.children[c] == null) {
14+
cur.children[c] = new Trie();
1415
}
15-
cur = cur.children[c - 'a'];
16+
cur = cur.children[c];
1617
}
1718
cur.root = root;
1819
}
1920
List<String> ans = new ArrayList<>();
20-
for (String word : sentence.split("\\s+")) {
21+
for (String word : sentence.split(" ")) {
2122
Trie cur = trie;
2223
for (char c : word.toCharArray()) {
23-
if (cur.children[c - 'a'] == null || cur.root != null) {
24+
c -= 'a';
25+
if (cur.children[c] == null || cur.root != null) {
2426
break;
2527
}
26-
cur = cur.children[c - 'a'];
28+
cur = cur.children[c];
2729
}
2830
ans.add(cur.root == null ? word : cur.root);
2931
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Trie:
2-
def __init__(self) -> None:
2+
def __init__(self):
33
self.children = [None] * 26
44
self.root = None
55

@@ -10,7 +10,7 @@ def replaceWords(self, dictionary: List[str], sentence: str) -> str:
1010
for root in dictionary:
1111
cur = trie
1212
for c in root:
13-
idx = ord(c) - ord('a')
13+
idx = ord(c) - ord("a")
1414
if cur.children[idx] is None:
1515
cur.children[idx] = Trie()
1616
cur = cur.children[idx]
@@ -20,9 +20,9 @@ def replaceWords(self, dictionary: List[str], sentence: str) -> str:
2020
for word in sentence.split():
2121
cur = trie
2222
for c in word:
23-
idx = ord(c) - ord('a')
24-
if cur.children[idx] is None or cur.root is not None:
23+
idx = ord(c) - ord("a")
24+
if cur.children[idx] is None or cur.root:
2525
break
2626
cur = cur.children[idx]
27-
ans.append(word if cur.root is None else cur.root)
28-
return ' '.join(ans)
27+
ans.append(cur.root or word)
28+
return " ".join(ans)

solution/2300-2399/2328.Number of Increasing Paths in a Grid/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757

5858
时间复杂度 $O(mn)$。
5959

60+
相似题目:[329. 矩阵中的最长递增路径](/solution/0300-0399/0329.Longest%20Increasing%20Path%20in%20a%20Matrix/README.md)
61+
6062
<!-- tabs:start -->
6163

6264
### **Python3**

0 commit comments

Comments
 (0)