Given a string s of length n, find the longest string t that occurs both forwards and backwards in s. e.g, s = yabcxqcbaz, then return t = abc or t = cba
I am considering using the generalized suffix tree but I think it would take me O(n^2) time.
i = 0 # Initialize the position on the S
j = 0 # Initialize the position on the Sr
n = len(S) # n is the length of the string
maxLengthPos = (0, 0) # Record the maximum length of such substring found so far and its position
# Iterate through every
for i in range(n):
for j in range(n):
maxL, pos = maxLengthPos
l = LCE(i, j) # The longest common extension which take O(1) time
if l > maxL:
maxLength = (l, i)
Can I implement it in O(n) time?