Find Substring With Given Hash Value LeetCode solution in Python
1# Runtime: 996 ms (Top 33.98%) | Memory: 14.1 MB (Top 94.23%)2class Solution:3def subStrHash(self, s: str, power: int, mod: int, k: int, hashValue: int) -> str:4val = lambda ch: ord(ch) - ord("a") + 15hash, res, power_k = 0, 0, pow(power, k, mod)6for i in reversed(range(len(s))):7hash = (hash * power + val(s[i])) % mod8if i < len(s) - k:9hash = (mod + hash - power_k * val(s[i + k]) % mod) % mod10res = i if hash == hashValue else res11return s[res : res + k]