The question is already in the title, what is the worst-case time complexity of the C implementation of str.find(string, substring)
in Python if n is the length of string
and m is the length of substring
? The source code (https://hg.python.org/cpython/file/99f5a0475ead/Objects/stringlib/fastsearch.h) seems to talk about the boyer-moore-horspool algorithm, which according to Wikipedia has a worst-case complexity of O(m*n).
EDIT: O(m*n) refers to the runnning time of the boyer-moore-horspool Algorithm, which finds all the occurrences of a substring in a string. Python's str.find
Method finds only one occurrence of the substring, so it's (str.find
) will depend on the position of the first occurrence of substring
. So NO, I haven't already posted the answer.
The source code seems to talk about the boyer-moore-horspool algorithm, which according to Wikipedia has a worst-case complexity of O(m*n).
Your answer is O(m*n)
for CPython. In general, it's obviously implementation-dependent.
EDIT: Yes, I wondered why you're asking this, if you already had made the research.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With