代码拉取完成,页面将自动刷新
# Time: O(n + m), m is the number of targets
# Space: O(n)
class Solution(object):
def findReplaceString(self, S, indexes, sources, targets):
"""
:type S: str
:type indexes: List[int]
:type sources: List[str]
:type targets: List[str]
:rtype: str
"""
bucket = [None] * len(S)
for i in xrange(len(indexes)):
if all(indexes[i]+k < len(S) and S[indexes[i]+k] == sources[i][k]
for k in xrange(len(sources[i]))):
bucket[indexes[i]] = (len(sources[i]), list(targets[i]))
result = []
i = 0
while i < len(S):
if bucket[i]:
result.extend(bucket[i][1])
i += bucket[i][0]
else:
result.append(S[i])
i += 1
return "".join(result)
# Time: O(mlogm + m * n)
# Space: O(n + m)
class Solution2(object):
def findReplaceString(self, S, indexes, sources, targets):
"""
:type S: str
:type indexes: List[int]
:type sources: List[str]
:type targets: List[str]
:rtype: str
"""
for i, s, t in sorted(zip(indexes, sources, targets), reverse=True):
if S[i:i+len(s)] == s:
S = S[:i] + t + S[i+len(s):]
return S
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。