Longest Palindromic Substring | Set 2
Given a string, find the longest substring which is a palindrome.
For Example:
Input: Given string :"forgeeksskeegfor", Output: "geeksskeeg". Input: Given string :"Geeks", Output: "ee".
Approach: Dynamic programming solution is already discussed here in the previous post. The time complexity of the Dynamic Programming based solution is O(n^2) and it requires O(n^2) extra space. We can find the longest palindrome substring( LPS ) in (n^2) time with O(1) extra space.
The algorithm below is very simple and easy to understand. The idea is to Fix a center and expand in both directions for longer palindromes and keep track of the longest palindrome seen so far.
ALGO:
- Maintain a variable ‘ maxLength = 1 ‘ (for storing LPS length) and ‘ start =0 ‘ (for storing starting index of LPS ).
- The idea is very simple, we will traverse through the entire string with i=0 to i<(length of string).
- while traversing, initialize ‘low‘ and ‘high‘ pointer such that low= i-1 and high= i+1.
- keep incrementing ‘high’ until str[high]==str[i] .
- similarly keep decrementing ‘low’ until str[low]==str[i].
- finally we will keep incrementing ‘high’ and decrementing ‘low’ until str[low]==str[high].
- calculate length=high-low-1, if length > maxLength then maxLength = length and start = low+1 .
- Print the LPS and return maxLength.
C++
// A O(n^2) time and O(1) space program to // find the longest palindromic substring // easy to understand as compared to previous version. #include <bits/stdc++.h> using namespace std; // A utility function to print // a substring str[low..high] // This function prints the // longest palindrome substring (LPS) // of str[]. It also returns the // length of the longest palindrome int longestPalSubstr(string str) { int n = str.size(); // calculating size of string if (n < 2) return n; // if string is empty then size will be 0. // if n==1 then, answer will be 1(single // character will always palindrome) int maxLength = 1,start=0; int low, high; for ( int i = 0; i < n; i++) { low = i - 1; high = i + 1; while ( high < n && str[high] == str[i]) //increment 'high' high++; while ( low >= 0 && str[low] == str[i]) // decrement 'low' low--; while (low >= 0 && high < n && str[low] == str[high]){ low--; high++; } int length = high - low - 1; if (maxLength < length) { maxLength = length; start=low+1; } } cout<< "Longest palindrome substring is: " ; cout<<str.substr(start,maxLength); return maxLength; } // Driver program to test above functions int main() { string str = "forgeeksskeegfor" ; cout << "\nLength is: " << longestPalSubstr(str) << endl; return 0; } // This is code is contributed by saurabh yadav |
C
// A O(n^2) time and O(1) space // program to find the longest // palindromic substring #include <stdio.h> #include <string.h> // A utility function to print // a substring str[low..high] void printSubStr( char * str, int low, int high) { for ( int i = low; i <= high; ++i) printf ( "%c" , str[i]); } // This function prints the longest // palindrome substring (LPS) // of str[]. It also returns the // length of the longest palindrome int longestPalSubstr( char * str) { int n = strlen (str); // calculating size of string if (n < 2) return n; // if string is empty then size will be 0. // if n==1 then, answer will be 1(single // character will always palindrome) int maxLength = 1,start=0; int low, high; for ( int i = 0; i < n; i++) { low = i - 1; high = i + 1; while ( high < n && str[high] == str[i]) //increment 'high' high++; while ( low >= 0 && str[low] == str[i]) // decrement 'low' low--; while (low >= 0 && high < n && str[low] == str[high]){ low--; // decrement low high++; // increment high } int length = high - low - 1; if (maxLength < length) { maxLength = length; start=low+1; } } printf ( "Longest palindrome substring is: " ); printSubStr(str,start,start+maxLength-1); return maxLength; } // Driver program to test above functions int main() { char str[] = "forgeeksskeegfor" ; printf ( "\nLength is: %d" , longestPalSubstr(str)); return 0; } // This is code is contributed by saurabh yadav |
Java
// Java implementation of O(n^2) // time and O(1) space method // to find the longest palindromic substring public class LongestPalinSubstring { // This function prints the // longest palindrome substring // (LPS) of str[]. It also // returns the length of the // longest palindrome static int longestPalSubstr(String str) { int n = str.length(); // calculcharAting size of string if (n < 2 ) return n; // if string is empty then size will be 0. // if n==1 then, answer will be 1(single // character will always palindrome) int maxLength = 1 ,start= 0 ; int low, high; for ( int i = 0 ; i < n; i++) { low = i - 1 ; high = i + 1 ; while ( high < n && str.charAt(high) == str.charAt(i)) //increment 'high' high++; while ( low >= 0 && str.charAt(low) == str.charAt(i)) // decrement 'low' low--; while (low >= 0 && high < n && str.charAt(low) == str.charAt(high) ){ low--; high++; } int length = high - low - 1 ; if (maxLength < length){ maxLength = length; start=low+ 1 ; } } System.out.print( "Longest palindrome substring is: " ); System.out.println(str.substring(start, start + maxLength )); return maxLength; } // Driver program to test above function public static void main(String[] args) { String str = "forgeeksskeegfor" ; System.out.println( "Length is: " + longestPalSubstr(str)); } } // This is code is contributed by saurabh yadav |
Python3
# A O(n ^ 2) time and O(1) space program to find the # longest palindromic substring # This function prints the longest palindrome substring (LPS) # of str[]. It also returns the length of the longest palindrome def longestPalSubstr(string): n = len (string) # calculating size of string if (n < 2 ): return n # if string is empty then size will be 0. # if n==1 then, answer will be 1(single # character will always palindrome) start = 0 maxLength = 1 for i in range (n): low = i - 1 high = i + 1 while (high < n and string[high] = = string[i] ): high = high + 1 while (low > = 0 and string[low] = = string[i] ): low = low - 1 while (low > = 0 and high < n and string[low] = = string[high] ): low = low - 1 high = high + 1 length = high - low - 1 if (maxLength < length): maxLength = length start = low + 1 print ( "Longest palindrome substring is:" ,end = " " ) print (string[start:start + maxLength]) return maxLength # Driver program to test above functions string = ( "forgeeksskeegfor" ) print ( "Length is: " + str (longestPalSubstr(string))) #This is code is contributed by saurabh yadav |
C#
// C# implementation of O(n^2) time // and O(1) space method to find the // longest palindromic substring using System; class GFG { // This function prints the longest // palindrome substring (LPS) of str[]. // It also returns the length of the // longest palindrome public static int longestPalSubstr( string str) { int n = str.Length; // calculcharAting size of string if (n < 2) return n; // if string is empty then size will be 0. // if n==1 then, answer will be 1(single // character will always palindrome) int maxLength = 1,start=0; int low, high; for ( int i = 0; i < n; i++) { low = i - 1; high = i + 1; while ( high < n && str[high] == str[i] ) //increment 'high' high++; while ( low >= 0 && str[low] == str[i]) // decrement 'low' low--; while (low >= 0 && high < n && str[low] == str[high] ){ low--; high++; } int length = high - low - 1; if (maxLength < length){ maxLength = length; start=low+1; } } Console.Write( "Longest palindrome substring is: " ); Console.WriteLine(str.Substring(start, maxLength)); return maxLength; } // Driver Code public static void Main( string [] args) { string str = "forgeeksskeegfor" ; Console.WriteLine( "Length is: " + longestPalSubstr(str)); } } // This is code is contributed by saurabh yadav |
Javascript
<script> // A O(n^2) time and O(1) space program to // find the longest palindromic substring // easy to understand as compared to previous version. // A utility function to print // a substring str[low..high] // This function prints the // longest palindrome substring (LPS) // of str[]. It also returns the // length of the longest palindrome function longestPalSubstr(str) { let n = str.length; // calculating size of string if (n < 2) return n; // if string is empty then size will be 0. // if n==1 then, answer will be 1(single // character will always palindrome) let maxLength = 1,start=0; let low, high; for (let i = 0; i < n; i++) { low = i - 1; high = i + 1; while ( high < n && str[high] == str[i]) //increment 'high' high++; while ( low >= 0 && str[low] == str[i]) // decrement 'low' low--; while (low >= 0 && high < n && str[low] == str[high]){ low--; high++; } let length = high - low - 1; if (maxLength < length) { maxLength = length; start=low+1; } } document.write( "Longest palindrome substring is: " ); document.write(str.substring(start,maxLength+start)); return maxLength; } // Driver program to test above functions let str = "forgeeksskeegfor" ; document.write( "</br>" , "Length is: " + longestPalSubstr(str), "</br>" ); // This is code is contributed by shinjanpatra </script> |
Output:
Longest palindrome substring is: geeksskeeg Length is: 10
Complexity Analysis:
- Time complexity: O(n^2), where n is the length of the input string.
Outer Loop that traverse through entire string, and Inner Loop that is used to expend from i . - Auxiliary Space: O(1).
No extra space is needed.
Please write comments if you find anything incorrect, or have more information about the topic discussed above.