-
Notifications
You must be signed in to change notification settings - Fork 0
/
833.find-and-replace-in-string.py
43 lines (31 loc) · 1.13 KB
/
833.find-and-replace-in-string.py
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
# https://leetcode.cn/problems/find-and-replace-in-string
class Solution1:
'''
Date: 2023.08.15
Pass/Error/Bug: 2/1/0
执行用时: 44 ms, 在所有 Python3 提交中击败了 69.51% 的用户
内存消耗:15.69 Mb, 在所有 Python3 提交中击败了 78.05% 的用户
'''
def findReplaceString(self, s: str, indices: List[int], sources: List[str], targets: List[str]) -> str:
new_s = ''
hash_dict = {}
for idx in range(len(indices)):
hash_dict[indices[idx]] = [ sources[idx], targets[idx] ]
for idx in sorted(hash_dict.keys()):
new_s = s[:idx]
break
gap_idx = idx
for idx in sorted(hash_dict.keys()):
sub_s, s_target = hash_dict[idx]
sub_l = len(sub_s)
if gap_idx != idx:
new_s += s[gap_idx:idx]
gap_idx = idx
if s[idx:idx+sub_l] == sub_s:
new_s += s_target
else:
new_s += s[idx:idx+sub_l]
gap_idx = idx+sub_l
if gap_idx < len(s):
new_s += s[gap_idx:]
return new_s