Find Substring With Given Hash Value
hard · verified · 26% accepted · 447 likes · top 4%
string · sliding window · rolling hash · hash function
Description
The hash of a 0-indexed string s of length k, given integers p and m, is computed using the following function:
- hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m.
Where val(s[i]) represents the index of s[i] in the alphabet from val('a') = 1 to val('z') = 26.
You are given a string s and the integers power, modulo, k, and hashValue. Return sub, the first substring of s of length k such that hash(sub, power, modulo) == hashValue.
The test cases will be generated such that an answer always exists.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Example 2:
Solution