在 Python 中使用三次替换函数的时间复杂度 O(n) 是多少

what is time complexity O(n) for replace function using thrice in Python

我试图在 python

中找到 str.replace() 内置函数的时间复杂度(大 O)

我知道最坏的时间是 O(nm)* 来找到一个子串但是如果我们使用替换三次在一行中

newstr = str1.replace(char1,'*').replace(char2,char1).replace("*",char2)

我正在尝试交换某个字符串中的 char1 和 char2,替代代码使用的是 O(n) 时间复杂度的 for 循环。 但是对于上面的代码,Big O会变成3倍,还是会变成n^3呢? 这有意义吗?

But for the above code, will the Big O become 3 times more, or will become n^3?

使用Big-O符号来量化运行时间意味着根据定义忽略常数因子。也就是说,三重替换版本可能具有比进行交换的单个手动编码循环更高的常数因子,但它们仍然是 O(n)。

交换的三重替换版本不会是 O(n^3)。