Longest Palindromic Substring | Set 1 | A C ++ solution for longest palindrome ; Function to print a substring str [ low . . high ] ; This function prints the longest palindrome substring It also returns the length of the longest palindrome ; get length of input string ; All substrings of length 1 are palindromes ; Nested loop to mark start and end index ; Check palindrome ; Palindrome ; return length of LPS ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSubStr ( string str , int low , int high ) { for ( int i = low ; i <= high ; ++ i ) cout << str [ i ] ; } int longestPalSubstr ( string str ) { int n = str . size ( ) ; int maxLength = 1 , start = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { for ( int j = i ; j < str . length ( ) ; j ++ ) { int flag = 1 ; for ( int k = 0 ; k < ( j - i + 1 ) / 2 ; k ++ ) if ( str [ i + k ] != str [ j - k ] ) flag = 0 ; if ( flag && ( j - i + 1 ) > maxLength ) { start = i ; maxLength = j - i + 1 ; } } } cout << " Longest ▁ palindrome ▁ substring ▁ is : ▁ " ; printSubStr ( str , start , start + maxLength - 1 ) ; return maxLength ; } int main ( ) { string str = " forgeeksskeegfor " ; cout << " Length is : " << longestPalSubstr ( str ) ; return 0 ; }