Search This Blog

Tuesday, February 25, 2014

Find Longest Palindrome in a string

__author__ = 'nitin'

given_word='anitins'

def isPalindrome(word):
    i = 0
    j = len(word) - 1
    while i < j:
        if word[i] != word[j]:
            return False
        i+=1
        j-=1
    return True

def longestPalindrome(string):
    length = len(string)
    maxLength = len(string)
    while maxLength > 1:
        start = 0
        while start <= length - maxLength:
            end = start + maxLength
            if isPalindrome(string[start:end]):
                return string[start:end]
            start += 1
        maxLength -= 1
    return False

if __name__=="__main__":
    print longestPalindrome(given_word)

No comments:

My Profile

My photo
can be reached at 09916017317