Python efficient way to check if very large string contains a substring

后端 未结 8 1847
無奈伤痛
無奈伤痛 2021-01-02 05:04

Python is not my best language, and so I\'m not all that good at finding the most efficient solutions to some of my problems. I have a very large string (coming from a 30 MB

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 05:48

    You can use one of these algorithms:

    • Rabin–Karp string search algorithm

    • Knuth–Morris–Pratt algorithm (aka KMP) see an implementation here

    Although I believe KMP is more efficient, it's more complicated to implement.The first link includes some pseudo-code that should make it very easy to implement in python.

    you can look for alternatives here: http://en.wikipedia.org/wiki/String_searching_algorithm

提交回复
热议问题