面试题答案
一键面试s = 'hello world, hello python'
new_s = s.replace('hello', 'hi')
print(new_s)
使用的replace
方法时间复杂度为$O(n)$,其中$n$为字符串的长度。因为该方法需要遍历整个字符串来查找并替换目标子串。
s = 'hello world, hello python'
new_s = s.replace('hello', 'hi')
print(new_s)
使用的replace
方法时间复杂度为$O(n)$,其中$n$为字符串的长度。因为该方法需要遍历整个字符串来查找并替换目标子串。