Saturday, May 29, 2010

How to find largest palindrome

in a string of lenght n

2 comments:

  1. largest palindrome of string length n is 2n, constructed by appending to the original string characters in the reverse order

    ReplyDelete
  2. 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.

    ReplyDelete