Home > Software engineering >  C string::rfind time complexicity
C string::rfind time complexicity

Time:09-21

What is time complexity of string::rfind method in c ? From what I understood, rfind is reversing the string and then doing string::find. Am I correct? Then it should be O(n)

CodePudding user response:

No, you are not correct. rfind searches from the back of the string without reversing the string. The complexity is the same as for a forward find.

I haven't found anything in the standard requiring this, but any search algorithm that you can use for find (like a naive search or a Boyer-Moore / Boyer-Moore-Horspool search) can also be used for rfind - so I find it highly unlikely that any library implementors would choose a less effective algorithm for their rfind implementation.

  • Related