【问题标题】:Time Complexity of String Comparison字符串比较的时间复杂度
【发布时间】:2019-03-25 01:47:41
【问题描述】:

我进行了一些测试以确定字符串的 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):例如,检查整数和小/固定长度的字符串。

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

【问题讨论】:

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


    【解决方案1】:

    算法很简单,你逐个字符检查字符串,所以:

    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.
    

    【讨论】:

    • 相等的字符串呢?会是 O(1) 吗?
    • @Hello.World 不,它们仍然是O(N),因为在您检查所有单个字符是否相等之前,您不知道它们是否相等。第一个字符对检查不同的唯一最佳情况是O(1)
    • 所以字符串的不变性绝不会影响相等性检查,对吧?
    • 在这种情况下不是,它们由于其他原因是不可变的。
    猜你喜欢
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多