Longest Palindromic Substring | Set 1
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"
Method 1: Brute Force.
Approach: The simple approach is to check each substring whether the substring is a palindrome or not. To do this first, run three nested loops, the outer two loops pick all substrings one by one by fixing the corner characters, the inner loop checks whether the picked substring is palindrome or not.
C++
// A C++ solution for longest palindrome #include <bits/stdc++.h> using namespace std; // Function to print a substring str[low..high] void printSubStr(string str, int low, int high) { for ( int i = low; i <= high; ++i) cout << str[i]; } // This function prints the // longest palindrome substring // It also returns the length // of the longest palindrome int longestPalSubstr(string str) { // get length of input string int n = str.size(); // All substrings of length 1 // are palindromes int maxLength = 1, start = 0; // Nested loop to mark start and end index for ( int i = 0; i < str.length(); i++) { for ( int j = i; j < str.length(); j++) { int flag = 1; // Check palindrome for ( int k = 0; k < (j - i + 1) / 2; k++) if (str[i + k] != str[j - k]) flag = 0; // Palindrome if (flag && (j - i + 1) > maxLength) { start = i; maxLength = j - i + 1; } } } cout << "Longest palindrome substring is: " ; printSubStr(str, start, start + maxLength - 1); // return length of LPS return maxLength; } // Driver Code int main() { string str = "forgeeksskeegfor" ; cout << "\nLength is: " << longestPalSubstr(str); return 0; } |
Java
// A Java solution for longest palindrome import java.util.*; class GFG{ // Function to print a subString str[low..high] static void printSubStr(String str, int low, int high) { for ( int i = low; i <= high; ++i) System.out.print(str.charAt(i)); } // This function prints the // longest palindrome subString // It also returns the length // of the longest palindrome static int longestPalSubstr(String str) { // get length of input String int n = str.length(); // All subStrings of length 1 // are palindromes int maxLength = 1 , start = 0 ; // Nested loop to mark start and end index for ( int i = 0 ; i < str.length(); i++) { for ( int j = i; j < str.length(); j++) { int flag = 1 ; // Check palindrome for ( int k = 0 ; k < (j - i + 1 ) / 2 ; k++) if (str.charAt(i + k) != str.charAt(j - k)) flag = 0 ; // Palindrome if (flag!= 0 && (j - i + 1 ) > maxLength) { start = i; maxLength = j - i + 1 ; } } } System.out.print( "Longest palindrome subString is: " ); printSubStr(str, start, start + maxLength - 1 ); // return length of LPS return maxLength; } // Driver Code public static void main(String[] args) { String str = "forgeeksskeegfor" ; System.out.print( "\nLength is: " + longestPalSubstr(str)); } } // This code is contributed by shikhasingrajput |
Python3
# A Python3 solution for longest palindrome # Function to pra subString str[low..high] def printSubStr( str , low, high): for i in range (low, high + 1 ): print ( str [i], end = "") # This function prints the # longest palindrome subString # It also returns the length # of the longest palindrome def longestPalSubstr( str ): # Get length of input String n = len ( str ) # All subStrings of length 1 # are palindromes maxLength = 1 start = 0 # Nested loop to mark start # and end index for i in range (n): for j in range (i, n): flag = 1 # Check palindrome for k in range ( 0 , ((j - i) / / 2 ) + 1 ): if ( str [i + k] ! = str [j - k]): flag = 0 # Palindrome if (flag ! = 0 and (j - i + 1 ) > maxLength): start = i maxLength = j - i + 1 print ( "Longest palindrome subString is: " , end = "") printSubStr( str , start, start + maxLength - 1 ) # Return length of LPS return maxLength # Driver Code if __name__ = = '__main__' : str = "forgeeksskeegfor" print ( "\nLength is: " , longestPalSubstr( str )) # This code is contributed by 29AjayKumar |
C#
// A C# solution for longest palindrome using System; class GFG{ // Function to print a subString str[low..high] static void printSubStr(String str, int low, int high) { for ( int i = low; i <= high; ++i) Console.Write(str[i]); } // This function prints the // longest palindrome subString // It also returns the length // of the longest palindrome static int longestPalSubstr(String str) { // get length of input String int n = str.Length; // All subStrings of length 1 // are palindromes int maxLength = 1, start = 0; // Nested loop to mark start and end index for ( int i = 0; i < str.Length; i++) { for ( int j = i; j < str.Length; j++) { int flag = 1; // Check palindrome for ( int k = 0; k < (j - i + 1) / 2; k++) if (str[i + k] != str[j - k]) flag = 0; // Palindrome if (flag!=0 && (j - i + 1) > maxLength) { start = i; maxLength = j - i + 1; } } } Console.Write( "longest palindrome subString is: " ); printSubStr(str, start, start + maxLength - 1); // return length of LPS return maxLength; } // Driver Code public static void Main(String[] args) { String str = "forgeeksskeegfor" ; Console.Write( "\nLength is: " + longestPalSubstr(str)); } } // This code is contributed by shikhasingrajput |
Javascript
<script> // A Javascript solution for longest palindrome // Function to print a subString str[low..high] function printSubStr(str,low,high) { for (let i = low; i <= high; ++i) document.write(str[i]); } // This function prints the // longest palindrome subString // It also returns the length // of the longest palindrome function longestPalSubstr(str) { // get length of input String let n = str.length; // All subStrings of length 1 // are palindromes let maxLength = 1, start = 0; // Nested loop to mark start and end index for (let i = 0; i < str.length; i++) { for (let j = i; j < str.length; j++) { let flag = 1; // Check palindrome for (let k = 0; k < (j - i + 1) / 2; k++) if (str[i + k] != str[j - k]) flag = 0; // Palindrome if (flag!=0 && (j - i + 1) > maxLength) { start = i; maxLength = j - i + 1; } } } document.write( "Longest palindrome subString is: " ); printSubStr(str, start, start + maxLength - 1); // return length of LPS return maxLength; } // Driver Code let str = "forgeeksskeegfor" ; document.write( "<br>Length is: " + longestPalSubstr(str)); // This code is contributed by rag2127 </script> |
Output
Longest palindrome substring is: geeksskeeg Length is: 10
Complexity Analysis: