代码拉取完成,页面将自动刷新
# Time: O(nlogn)
# Space: O(n)
# other solution is to apply kasai's algorithm, refer to the link below:
# https://leetcode.com/problems/longest-duplicate-substring/discuss/290852/Suffix-array-clear-solution
import collections
class Solution(object):
def longestDupSubstring(self, S):
"""
:type S: str
:rtype: str
"""
M = 10**9+7
D = 26
def check(S, L):
p = pow(D, L, M)
curr = reduce(lambda x, y: (D*x+ord(y)-ord('a')) % M, S[:L], 0)
lookup = collections.defaultdict(list)
lookup[curr].append(L-1)
for i in xrange(L, len(S)):
curr = ((D*curr) % M + ord(S[i])-ord('a') -
((ord(S[i-L])-ord('a'))*p) % M) % M
if curr in lookup:
for j in lookup[curr]: # check if string is the same when hash is the same
if S[j-L+1:j+1] == S[i-L+1:i+1]:
return i-L+1
lookup[curr].append(i)
return 0
left, right = 1, len(S)-1
while left <= right:
mid = left + (right-left)//2
if not check(S, mid):
right = mid-1
else:
left = mid+1
result = check(S, right)
return S[result:result + right]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。