String count with overlapping occurrences

前端 未结 22 3017
耶瑟儿~
耶瑟儿~ 2020-11-21 23:25

What\'s the best way to count the number of occurrences of a given string, including overlap in Python? This is one way:

def function(string, str_to_search_f         


        
22条回答
  •  伪装坚强ぢ
    2020-11-21 23:40

    If strings are large, you want to use Rabin-Karp, in summary:

    • a rolling window of substring size, moving over a string
    • a hash with O(1) overhead for adding and removing (i.e. move by 1 char)
    • implemented in C or relying on pypy

提交回复
热议问题