-3

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

2

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
  • 555,201
  • 31
  • 458
  • 770
Incisor
  • 328
  • 1
  • 16