DEV Community


Discussion on: The algorithm behind Ctrl + F.

aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

Step 2 seems flawed. What if a character occurs more than once in the pattern? Then you'll only store the last index.

akhilpokle profile image
Akhil Author

Yep, that's the beauty of the algorithm since we will store the last index, the "skip" will be large. Since the skip is large if the characters from the end do not match, we will skip even more characters bringing down the overall number of comparisons.