(review) leetcode 2156. find substring with given hash value (weekly contest 278)
https://leetcode.com/contest/weekly-contest-278/problems/find-substring-with-given-hash-value/
1st try: time limit exceed
2nd try: time limit exceed (pre computing)
3rd try: pass (pre computing more)
1st try: time limit exceed
2nd try: time limit exceed (pre computing)
3rd try: pass (pre computing more)
class Solution:
def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
xa = [1+ord(ch)-ord('a') for ch in s]
xb = [1] * (k+1)
for i in range(1, k+1):
xb[i] = (xb[i-1] * power) % modulo
n = len(xa)
h = sum(xa[n-k+i]*xb[i] for i in range(k))
r = n-k
for i in range(n-k-1, -1, -1):
h = (h*power + xa[i] - xa[i+k]*xb[k]) % modulo
if h == hashValue:
r = i
return s[r:r+k]
Reference
この問題について((review) leetcode 2156. find substring with given hash value (weekly contest 278)), 我々は、より多くの情報をここで見つけました https://velog.io/@jhcho/review-leetcode-2156.-find-substring-with-given-hash-valueテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol