Skip to content

Commit fdd3730

Browse files
committed
feat: add solutions to lc problem: No.0833
No.0833.Find And Replace in String
1 parent 9e7fb59 commit fdd3730

File tree

6 files changed

+305
-2
lines changed

6 files changed

+305
-2
lines changed

solution/0800-0899/0833.Find And Replace in String/README.md

+110-1
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,131 @@
7070

7171
<!-- 这里可写通用的实现逻辑 -->
7272

73+
**方法一:模拟**
74+
75+
我们先遍历 `indices`,对于每个 $i$,如果 `s[indices[i]: indices[i] + len(sources[i])] == sources[i]`,则说明 $s$ 中从 `indices[i]` 开始的 `len(sources[i])` 个字符与 `sources[i]` 相等,我们记录下标 `indices[i]` 处需要替换的是 `targets[i]`,否则不需要替换。
76+
77+
然后我们从左到右遍历 $s$,如果当前下标 $i$ 处需要替换,则将 `targets[d[i]]` 加入答案,并且 $i$ 跳过 `len(sources[d[i]])` 个字符,否则将 `s[i]` 加入答案,然后 $i$ 自增 $1$。
78+
79+
时间复杂度 $O(k + n)$,空间复杂度 $O(n)$。其中 $k$ 和 $n$ 分别是 `indices` 和 $s$ 的长度。
80+
7381
<!-- tabs:start -->
7482

7583
### **Python3**
7684

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

7987
```python
80-
88+
class Solution:
89+
def findReplaceString(self, s: str, indices: List[int], sources: List[str], targets: List[str]) -> str:
90+
n = len(s)
91+
d = [-1] * n
92+
for i, (j, source) in enumerate(zip(indices, sources)):
93+
if s[j: j + len(source)] == source:
94+
d[j] = i
95+
ans = []
96+
i = 0
97+
while i < n:
98+
if d[i] >= 0:
99+
ans.append(targets[d[i]])
100+
i += len(sources[d[i]])
101+
else:
102+
ans.append(s[i])
103+
i += 1
104+
return ''.join(ans)
81105
```
82106

83107
### **Java**
84108

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

87111
```java
112+
class Solution {
113+
public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
114+
int n = s.length();
115+
int[] d = new int[n];
116+
Arrays.fill(d, -1);
117+
for (int i = 0; i < indices.length; ++i) {
118+
int j = indices[i];
119+
String source = sources[i];
120+
if (s.substring(j, Math.min(n, j + source.length())).equals(source)) {
121+
d[j] = i;
122+
}
123+
}
124+
StringBuilder ans = new StringBuilder();
125+
for (int i = 0; i < n; ) {
126+
if (d[i] >= 0) {
127+
ans.append(targets[d[i]]);
128+
i += sources[d[i]].length();
129+
} else {
130+
ans.append(s.charAt(i++));
131+
}
132+
}
133+
return ans.toString();
134+
}
135+
}
136+
```
137+
138+
### **C++**
139+
140+
```cpp
141+
class Solution {
142+
public:
143+
string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets) {
144+
int n = s.size();
145+
vector<int> d(n, -1);
146+
for (int i = 0; i < indices.size(); ++i) {
147+
int j = indices[i];
148+
string source = sources[i];
149+
if (s.substr(j, source.size()) == source) {
150+
d[j] = i;
151+
}
152+
}
153+
string ans;
154+
for (int i = 0; i < n;) {
155+
if (d[i] >= 0) {
156+
ans += targets[d[i]];
157+
i += sources[d[i]].size();
158+
} else {
159+
ans += s[i++];
160+
}
161+
}
162+
return ans;
163+
}
164+
};
165+
```
88166
167+
### **Go**
168+
169+
```go
170+
func findReplaceString(s string, indices []int, sources []string, targets []string) string {
171+
n := len(s)
172+
d := make([]int, n)
173+
for i, j := range indices {
174+
source := sources[i]
175+
if s[j:min(j+len(source), n)] == source {
176+
d[j] = i + 1
177+
}
178+
}
179+
ans := &strings.Builder{}
180+
for i := 0; i < n; {
181+
if d[i] > 0 {
182+
ans.WriteString(targets[d[i]-1])
183+
i += len(sources[d[i]-1])
184+
} else {
185+
ans.WriteByte(s[i])
186+
i++
187+
}
188+
}
189+
return ans.String()
190+
}
191+
192+
func min(a, b int) int {
193+
if a < b {
194+
return a
195+
}
196+
return b
197+
}
89198
```
90199

91200
### **...**

solution/0800-0899/0833.Find And Replace in String/README_EN.md

+102-1
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,114 @@
6767
### **Python3**
6868

6969
```python
70-
70+
class Solution:
71+
def findReplaceString(self, s: str, indices: List[int], sources: List[str], targets: List[str]) -> str:
72+
n = len(s)
73+
d = [-1] * n
74+
for i, (j, source) in enumerate(zip(indices, sources)):
75+
if s[j: j + len(source)] == source:
76+
d[j] = i
77+
ans = []
78+
i = 0
79+
while i < n:
80+
if d[i] >= 0:
81+
ans.append(targets[d[i]])
82+
i += len(sources[d[i]])
83+
else:
84+
ans.append(s[i])
85+
i += 1
86+
return ''.join(ans)
7187
```
7288

7389
### **Java**
7490

7591
```java
92+
class Solution {
93+
public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
94+
int n = s.length();
95+
int[] d = new int[n];
96+
Arrays.fill(d, -1);
97+
for (int i = 0; i < indices.length; ++i) {
98+
int j = indices[i];
99+
String source = sources[i];
100+
if (s.substring(j, Math.min(n, j + source.length())).equals(source)) {
101+
d[j] = i;
102+
}
103+
}
104+
StringBuilder ans = new StringBuilder();
105+
for (int i = 0; i < n; ) {
106+
if (d[i] >= 0) {
107+
ans.append(targets[d[i]]);
108+
i += sources[d[i]].length();
109+
} else {
110+
ans.append(s.charAt(i++));
111+
}
112+
}
113+
return ans.toString();
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets) {
124+
int n = s.size();
125+
vector<int> d(n, -1);
126+
for (int i = 0; i < indices.size(); ++i) {
127+
int j = indices[i];
128+
string source = sources[i];
129+
if (s.substr(j, source.size()) == source) {
130+
d[j] = i;
131+
}
132+
}
133+
string ans;
134+
for (int i = 0; i < n;) {
135+
if (d[i] >= 0) {
136+
ans += targets[d[i]];
137+
i += sources[d[i]].size();
138+
} else {
139+
ans += s[i++];
140+
}
141+
}
142+
return ans;
143+
}
144+
};
145+
```
76146
147+
### **Go**
148+
149+
```go
150+
func findReplaceString(s string, indices []int, sources []string, targets []string) string {
151+
n := len(s)
152+
d := make([]int, n)
153+
for i, j := range indices {
154+
source := sources[i]
155+
if s[j:min(j+len(source), n)] == source {
156+
d[j] = i + 1
157+
}
158+
}
159+
ans := &strings.Builder{}
160+
for i := 0; i < n; {
161+
if d[i] > 0 {
162+
ans.WriteString(targets[d[i]-1])
163+
i += len(sources[d[i]-1])
164+
} else {
165+
ans.WriteByte(s[i])
166+
i++
167+
}
168+
}
169+
return ans.String()
170+
}
171+
172+
func min(a, b int) int {
173+
if a < b {
174+
return a
175+
}
176+
return b
177+
}
77178
```
78179

79180
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets) {
4+
int n = s.size();
5+
vector<int> d(n, -1);
6+
for (int i = 0; i < indices.size(); ++i) {
7+
int j = indices[i];
8+
string source = sources[i];
9+
if (s.substr(j, source.size()) == source) {
10+
d[j] = i;
11+
}
12+
}
13+
string ans;
14+
for (int i = 0; i < n;) {
15+
if (d[i] >= 0) {
16+
ans += targets[d[i]];
17+
i += sources[d[i]].size();
18+
} else {
19+
ans += s[i++];
20+
}
21+
}
22+
return ans;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func findReplaceString(s string, indices []int, sources []string, targets []string) string {
2+
n := len(s)
3+
d := make([]int, n)
4+
for i, j := range indices {
5+
source := sources[i]
6+
if s[j:min(j+len(source), n)] == source {
7+
d[j] = i + 1
8+
}
9+
}
10+
ans := &strings.Builder{}
11+
for i := 0; i < n; {
12+
if d[i] > 0 {
13+
ans.WriteString(targets[d[i]-1])
14+
i += len(sources[d[i]-1])
15+
} else {
16+
ans.WriteByte(s[i])
17+
i++
18+
}
19+
}
20+
return ans.String()
21+
}
22+
23+
func min(a, b int) int {
24+
if a < b {
25+
return a
26+
}
27+
return b
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
3+
int n = s.length();
4+
int[] d = new int[n];
5+
Arrays.fill(d, -1);
6+
for (int i = 0; i < indices.length; ++i) {
7+
int j = indices[i];
8+
String source = sources[i];
9+
if (s.substring(j, Math.min(n, j + source.length())).equals(source)) {
10+
d[j] = i;
11+
}
12+
}
13+
StringBuilder ans = new StringBuilder();
14+
for (int i = 0; i < n; ) {
15+
if (d[i] >= 0) {
16+
ans.append(targets[d[i]]);
17+
i += sources[d[i]].length();
18+
} else {
19+
ans.append(s.charAt(i++));
20+
}
21+
}
22+
return ans.toString();
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def findReplaceString(self, s: str, indices: List[int], sources: List[str], targets: List[str]) -> str:
3+
n = len(s)
4+
d = [-1] * n
5+
for i, (j, source) in enumerate(zip(indices, sources)):
6+
if s[j: j + len(source)] == source:
7+
d[j] = i
8+
ans = []
9+
i = 0
10+
while i < n:
11+
if d[i] >= 0:
12+
ans.append(targets[d[i]])
13+
i += len(sources[d[i]])
14+
else:
15+
ans.append(s[i])
16+
i += 1
17+
return ''.join(ans)

0 commit comments

Comments
 (0)