字符串比较的时间复杂度

时间:2019-03-25 01:47:41

标签: python python-3.x string time-complexity

我进行了一些测试,以确定字符串的O(==)是O(len(string))还是O(1)。

我的测试:

import timeit
x = 'ab' * 500000000
y = 'ab' * 500000000
%timeit x == y

> 163 ms ± 4.62 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

x = 'ab' * 5000
y = 'ab' * 5000
%timeit x == y

> 630 ns ± 23.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

通过查看以上结果,我知道字符串比较是线性O(N)而不是O(1)。

但是,我正在阅读以下文档:Complexities of Python Operations

部分:

最后,当比较两个列表的相等性时,上面的复杂度类显示为O(N),但实际上,我们需要将此复杂度类乘以O ==(...),其中O == (...)是用于检查列表中的两个值是否为==的复杂度类。如果它们是整数,则O ==(...)将为O(1);如果它们是字符串,则O ==(...)在最坏的情况下将是O(len(string))。只要完成==检查,就会出现此问题。我们通常会假设==检查列表中的值是O(1):例如,检查int和小/定长字符串。

这表示字符串的最坏情况是O(len(string))。我的问题是为什么最坏的情况?最佳/平均情况不应该是O(len(string))吗?

1 个答案:

答案 0 :(得分:2)

算法很简单,您按char检查字符串char,所以:

Hello == Hello => They are equal...so it is actually the worst case because you check all the chars from both strings
Hello != Hella => Still worst case, you realize they are different in the last char of the strings.
hello != Hello => Best case scenario, the first char for both (h != H) are different, so you stop checking them there.