Find Longest Palindromic Substring – SET 1
“Find Longest Palindromic Substring” is an important and one of the interview favourite problem of string data structure. Here, we are given a string and our task is to write a program to find the longest palindromic substring in the given string.
Longest Palindromic Substring: Longest Palindromic Substring of the given string is a substring of the string which is also palindrome. We need to find the largest possible substring of such kind.
If there is no palindromic substring of length greater than 1 present in the string, simple return first character of the string.
Example:
INPUT: abcba OUTPUT: Longest Palindromic Substring: abcba Explanation: “abcba” is itself palindromic string. INPUT: abcded OUTPUT: Longest Palindromic Substring: ded Explanation: “ded” is the longest substring in the string “abcded” which is palindromic.
In this post, we will discuss the brute-force approach to get the solution. The steps required to find longest palindromic substring are:
- Initialize an empty resultant string.
- Generate all possible substring of the given string.
- For every substring generated, check whether it is palindrome or not.
- If it is palindrome, check its length with resultant string length.
- If it’s greater, assign the resultant string with generated palindromic substring.
The time complexity of this solution is O(n4) for generating all substring and check palindrome.
C++ Program to Find Longest Palindromic Substring in a given string is as follows:
/* C++ Program to Find Longest Palindromic Substring in a given string */
#include<bits/stdc++.h>
using namespace std;
bool isPalindrome(string str)
{
string temp = str;
reverse(temp.begin(),temp.end());
return (temp==str) ? true : false ;
}
void longestPalindrome(string str)
{
// Single characterb is itself a palindrome
string res = str.substr(0,1);
//Generate all possible substrings of a given string
for(int i = 0 ; i < str.length() ; i++)
{
for(int j = i ; j < str.length() ; j++)
{
string substring = "";
for(int k = i ; k <= j ; k++)
{
substring = substring + str[k];
}
// check whether generated substring is palindrome or not
bool checkPalindrome = isPalindrome(substring);
// If palindrome, compare its length with resultant palindrome
if(checkPalindrome)
{
if(substring.length() > res.length())
res = substring;
}
}
}
cout<<"Longest Palindromic Substring: "<<res<<"\n";
}
int main()
{
string str;
/* Scan String */
cout<<"Enter a string:\n";
cin>>str;
/* Function to find Longest Palindromic Substring */
longestPalindrome(str);
}
OUTPUT: Enter a string: abcded Longest Palindromic Substring: ded
Related Posts:
- Program to check Anagram Strings.
- Program to check whether two Strings are Rotation of each other or not.
- Program to check Palindromic Anagram.
- Program to print all the Palindromic Substring of the String.
- Program to check Panagram String.
- Program to find first non-repeating character of the String.
- Program to Reverse a String.
- Program to Count Number of Characters in a String.
- Program to Count frequency of particular character of the String.
- Program to Count Number of Vowels and Number of Consonants in a String.
- Program to Check if String is alphabetic or numeric or alpha-numeric.
- Program to copy one String to Another.
- Program to Concatenate Two Strings.
- Program to Check whether given String is Palindrome or not.
- Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
- Program to count total number of words in a string.
- Program to find smallest and largest word of the String.
- Program to find Most Frequent Character of the String.
- Program to Remove all the blank spaces from the String.
- Program to check if String is isogram or not.