I was working on LeetCode problem Longest Palindromic Substring and had the following code:
def longestPalindrome(s):
"""
:type s: str
:rtype: str
"""
length = len(s)
lookup = [[False for i in range(1000)] for j in range(1000)]
for i in range(0,length):
lookup[i][i] = True
longestBegin = i
maxL = 1
for i in range(0,length-1):
if s[i] == s[i+1]:
lookup[i][i+1] = True
longestBegin = i
maxL = 2
for len in range(3,n+1):
i = 0
while i <= n-len+1:
j = i + len -1
if lookup[i+1][j-1] and s[i] == s[j]:
lookup[i][j] = True
longestBegin = i
maxL = len
return s[longestBegin:longestBegin+maxL]
When i call the function:longestPalindrome('abdad')
, I got error:
"Runtime Error Message: Line 7: UnboundLocalError: local variable 'len' referenced before assignment".
Any help would be appreciated.