Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Longest Palindromic Substring using DP

# The value of table[i][j] is true, if the substring is palindrome, otherwise false.
# To calculate table[i][j], check the value of table[i+1][j-1], if the value is true and str[i] is same as str[j], then we make table[i][j] true.


# Python program

import sys

# A utility function to print a
# substring str[low..high]
def printSubStr(st, low, high) :
    sys.stdout.write(st[low : high + 1])
    sys.stdout.flush()
    return ''

# This function prints the longest palindrome
# substring of st[]. It also returns the length
# of the longest palindrome
def longestPalSubstr(st) :
    n = len(st) # get length of input string

    # table[i][j] will be false if substring 
    # str[i..j] is not palindrome. Else 
    # table[i][j] will be true
    table = [[0 for x in range(n)] for y
                          in range(n)] 
    
    # All substrings of length 1 are
    # palindromes
    maxLength = 1
    i = 0
    while (i < n) :
        table[i][i] = True
        i = i + 1
    
    # check for sub-string of length 2.
    start = 0
    i = 0
    while i < n - 1 :
        if (st[i] == st[i + 1]) :
            table[i][i + 1] = True
            start = i
            maxLength = 2
        i = i + 1
    
    # Check for lengths greater than 2. 
    # k is length of substring
    k = 3
    while k <= n :
        # Fix the starting index
        i = 0
        while i < (n - k + 1) :
            
            # Get the ending index of 
            # substring from starting 
            # index i and length k
            j = i + k - 1
    
            # checking for sub-string from
            # ith index to jth index iff 
            # st[i + 1] to st[(j-1)] is a 
            # palindrome
            if (table[i + 1][j - 1] and 
                      st[i] == st[j]) :
                table[i][j] = True
    
                if (k > maxLength) :
                    start = i
                    maxLength = k
            i = i + 1
        k = k + 1
    print "Longest palindrome substring is: ", printSubStr(st, start,
                                               start + maxLength - 1)

    return maxLength # return length of LPS


# Driver program to test above functions
st = "forgeeksskeegfor"
l = longestPalSubstr(st)
print "Length is:", l

# This code is contributed by Nikita Tiwari.
Comment

PREVIOUS NEXT
Code Example
Python :: python floor float 
Python :: clear 
Python :: python how to switch between true and false 
Python :: insert in python 
Python :: python remove character from string 
Python :: change the format of date in python 
Python :: how to index lists in python 
Python :: object oriented programming python 
Python :: Use operator in python list 
Python :: e in python 
Python :: merge list elements python 
Python :: how to standardize the image data to have values between 0 and 1 
Python :: create payment request in stripe 
Python :: how to replace a character of a string 
Python :: python json 
Python :: sparse matrix multiplication in python 
Python :: write hexadecimal in python 
Python :: Python NumPy ndarray flatten Function Syntax 
Python :: how to run other python files in python 
Python :: return max(max(a,b),max(c,d)); 
Python :: python array of objects 
Python :: matrix multiplication python without numpy 
Python :: python string and integer concatenation 
Python :: string length python 
Python :: how to make loop python 
Python :: queryset django 
Python :: Showing all column names and indexes dataframe python 
Python :: flask echo server 
Python :: join mulitple dataframe pandas index 
Python :: django creat app return _bootstrap._gcd_import 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =