Construct a matrix with the string and the reverse of it:
def palindromeMatrix(str): pMatrix = [] strT = str[::-1] for i in range(len(str)): tList = [] for j in range(len(str)): if str[i]==strT[j]: tList.append(1) else: tList.append(0) pMatrix.append(tList) print pMatrix
m = palindromeMatrix("mammal")
The 1's on any of the diagonals on the falling edge correspond to the largest palindrome sequence.
largest palindrome of string length n is 2n, constructed by appending to the original string characters in the reverse order
ReplyDeleteConstruct a matrix with the string and the reverse of it:
ReplyDeletedef palindromeMatrix(str):
pMatrix = []
strT = str[::-1]
for i in range(len(str)):
tList = []
for j in range(len(str)):
if str[i]==strT[j]:
tList.append(1)
else:
tList.append(0)
pMatrix.append(tList)
print pMatrix
m = palindromeMatrix("mammal")
The 1's on any of the diagonals on the falling edge correspond to the largest palindrome sequence.