Rabin Karp Algorithm

Strings (Advanced Algo) Advanced Problems (Less asked) Hard
  • Fun Fact: The Rabin-Karp algorithm is largely used in various programming applications like plagiarism detection
  • In most modern text editors and Integrated Development Environments (IDEs), Rabin-Karp string searching algorithm is used due to its efficiency in searching for duplicate strings or text
  • Its capability of detecting multiple pattern matches makes it widely used in data analysis and manipulation tools as well

Given a string text and a string pattern, implement the Rabin-Karp algorithm to find the starting index of all occurrences of pattern in text. If pattern is not found, return an empty list.

Examples:

Input: text = "ababcabcababc", pattern = "abc"


Output: [2, 5, 10]


Expalanation : The pattern "abc" is found at indices 2, 5, and 10 in the text.

Input: text = "hello", pattern = "ll"


Output: [2]


Explanation: The pattern "ll" is found at index 2 in the text.

Input: text = "abcdef", pattern = "gh"

Constraints

  • 1<= text.length, pattern.length <=5*104

Hints

  • "Compute the hash of the pattern and the first window (substring of same length) in text. Slide the window across text, updating the hash incrementally (rolling hash). If a hash match occurs, verify the actual substring (to avoid false positives due to hash collisions). Store the starting index if pattern matches."
  • "The hash function is computed as: H=(i=0 ∑m−1 ord(s[i])×base (m−i−1))modprime, where base (e.g., 31 or 101) and prime (e.g., large prime like 1e9+7) are chosen for minimizing hash collisions. The rolling hash update (for next window) is: H new =(H old−ord(s[i])×base (m−1))×base+ord(s[i+m])."

Company Tags

Siemens Healthineers Bain & Company Broadcom Deloitte Instacart Swiggy Databricks OYO Rooms Morgan Stanley Visa PwC Reddit Western Digital Mastercard Rockstar Games Flipkart Docker Medtronic Unity Technologies Roblox Uber AMD Bloomberg IBM Nutanix Google Microsoft Amazon Meta Apple Netflix Adobe