Skip to content

Commit fa3d5a8

Browse files
authored
Update find-and-replace-in-string.py
1 parent daad5cf commit fa3d5a8

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Python/find-and-replace-in-string.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@ def findReplaceString(self, S, indexes, sources, targets):
1010
:type targets: List[str]
1111
:rtype: str
1212
"""
13-
S = list(S)
1413
bucket = [None] * len(S)
1514
for i in xrange(len(indexes)):
16-
if all(indexes[i]+k < len(S) and
17-
S[indexes[i]+k] == sources[i][k]
15+
if all(indexes[i]+k < len(S) and S[indexes[i]+k] == sources[i][k]
1816
for k in xrange(len(sources[i]))):
1917
bucket[indexes[i]] = (len(sources[i]), list(targets[i]))
2018
result = []
21-
last = 0
22-
for i in xrange(len(S)):
19+
i = 0
20+
while i < len(S):
2321
if bucket[i]:
2422
result.extend(bucket[i][1])
25-
last = i + bucket[i][0]
26-
elif i >= last:
23+
i += bucket[i][0]
24+
else:
2725
result.append(S[i])
26+
i += 1
2827
return "".join(result)
2928

3029

0 commit comments

Comments
 (0)