-4

I need to search for a sub-string in a given string. C++ provides library function 'find' for this. What is the algorithm used by this library function? For purpose of competitive programming, should I use this function or use KMP search algorithm?

1 Answers1

1

With a quick search, I found this:

  • KMP is O(n + k)
  • Find is O(n * k)

So KMP is a faster search algorithm.

C++ string::find complexity

Knuth–Morris–Pratt algorithm

Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696
Incisor
  • 168
  • 15