Python string 'in' operator implementation algorithm and time complexity

I think about how the operator is implemented in, for example

>>> s1 = 'abcdef'
>>> s2 = 'bcd'
>>> s2 in s1
True

In CPython, which algorithm is used to implement string matching, and what is the time complexity? Is there an official document or wiki about this?

+16
source share
1 answer

This is a combination of Boyer-Moore and Horspool .

You can view the C code here :

/, - , . . http://effbot.org/zone/stringlib.htm.

:

:

  • , ( ),
  • ; (O (m) , O (1) )
  • (O (n/m))
  • (O (nm))
  • 8- , 16- 32- Unicode ( O (σ))
  • ,
+20

Source: https://habr.com/ru/post/1627411/


All Articles