Longest Repeated Substring with Rolling Hashes

medium
Unit 14 · Strings & BFS0/3 to milestone

Find the longest substring that occurs at least twice in a string (occurrences may overlap). A brute-force check is O(n³). Combine:

  • binary search on the length L (if some substring of length L repeats, so does one of length L − 1), and
  • a Rabin–Karp rolling hash to find repeats of length L in O(n), verifying candidate matches by direct comparison so hash collisions can't give wrong answers.
Pro problem

Unlock this and every premium problem across all tracks — plus hidden-test grading, advanced AI engineering modules and new content every week.

Start 7-day free trial
solution.py · Python 3.13
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Longest Repeated Substring with Rolling Hashes · TechGradify