Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Quasilinear solution in Speedy category for The Longest Palindromic by veky
def longest_palindromic(q):
def to(e):
for j in range(len(l) - 2, e, -1):
d = j - e - 1
k = l[j]
yield d, k
l.append(min(d, k))
l = []
i = p = 0
while i < len(q):
if i > p and q[i + ~p] == q[i]:
p += 2
i += 1
else:
l.append(p)
for d, k in to(len(l) - 2 - p):
if d == k:
p = d
break
else:
p = 1
i += 1
l.append(p)
list(to(2 * (len(l) - len(q)) - 3))
p = max(l)
c = l.index(p)
return q[c - p >> 1 : c + p >> 1]
March 3, 2016
Comments: