Technique
Rolling / Polynomial String Hashing
Hash every substring in O(1) after O(n) prefix-hash precomputation, enabling substring equality checks, often combined with binary search on length.
Learn firstString Matching (KMP / Z / Rabin-Karp)Prefix SumUnlocksSuffix Array + LCP
0%
mastery
0/7 solved
0/7 solved
Recognise it
The signal
compare many substrings
1Brute force
Compare substrings character by character.
2The observation
O(length) comparisons: equal hashes mean equal strings with high probability.
3Why it works
H[i] = sum s[j] * p^(i-1-j) mod M, so hash(l, r) = H[r] - H[l] * p^(r-l).
Reach for it when
- +longest repeated / common substring with binary search on length
- +count distinct substrings of a fixed length
Not when
- ×anti-hash tests on CF with fixed base/mod
- ×exactness required (use suffix-array / KMP)
Practice
0/7 solved
- Repeated DNA SequencesMedium
- Palindrome QueriesCSESMedium
- Prefix-Suffix Palindrome (Hard version)Codeforces1800
- Longest Duplicate SubstringHard
- Repeating SubstringCSESHard
- Compress WordsCodeforces2000
- Longest Happy PrefixHard
Pitfalls
Misconceptions
- !one 32-bit modulus with many comparisons collides (birthday bound)