Rabin-Karp Algorithm | Data Structures Interview | Skill-Lync Resources
Medium Data Structures Arrays & Strings

Explain the Rabin-Karp algorithm for string matching.

Answer

Rabin-Karp uses rolling hash to find pattern in text. Compute hash of pattern and first window of text. Slide window, updating hash in O(1) using rolling hash: remove contribution of outgoing char, add incoming char. When hashes match, verify with character comparison (to handle hash collisions). Average O(n+m) time but O(nm) worst case. Efficient for multiple pattern search.

Master These Concepts with IIT Certification
IIT Certified

Master These Concepts with IIT Certification

175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.

Relevant for Roles

Software Engineer Backend Developer Algorithm Developer