Maximum length palindromic substring such that it starts and ends with given char
Last Updated :
08 Jun, 2022
Given a string str and a character ch , the task is to find the longest palindromic sub-string of str such that it starts and ends with the given character ch .Examples:
Input: str = “lapqooqpqpl”, ch = ‘p’ Output: 6 “pqooqp” is the maximum length palindromic sub-string that starts and ends with ‘p’.Input: str = “geeksforgeeks”, ch = ‘k’ Output: 1 “k” is the valid sub-string.
Approach: For every possible index pair (i, j) such that str[i] = str[j] = ch check whether the sub-string str[i…j] is palindrome or not. For all the found palindromes, store the length of the longest palindrome found so far. Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string str, int i, int j)
{
while (i < j) {
if (str[i] != str[j])
return false;
i++;
j--;
}
return true;
}
int maxLenPalindrome(string str, int n, char ch)
{
int maxLen = 0;
for (int i = 0; i < n; i++) {
if (str[i] == ch) {
for (int j = n - 1; j >= i; j--) {
if (str[j] == ch) {
if (isPalindrome(str, i, j)) {
maxLen = max(maxLen, j - i + 1);
break;
}
}
}
}
}
return maxLen;
}
int main()
{
string str = "lapqooqpqpl";
int n = str.length();
char ch = 'p';
cout << maxLenPalindrome(str, n, ch);
return 0;
}
Java
class GFG
{
static boolean isPalindrome(String str,
int i, int j)
{
while (i < j)
{
if (str.charAt(i) != str.charAt(j))
{
return false;
}
i++;
j--;
}
return true;
}
static int maxLenPalindrome(String str, int n, char ch)
{
int maxLen = 0;
for (int i = 0; i < n; i++)
{
if (str.charAt(i) == ch)
{
for (int j = n - 1; j >= i; j--)
{
if (str.charAt(j) == ch)
{
if (isPalindrome(str, i, j))
{
maxLen = Math.max(maxLen, j - i + 1);
break;
}
}
}
}
}
return maxLen;
}
public static void main(String[] args)
{
String str = "lapqooqpqpl";
int n = str.length();
char ch = 'p';
System.out.println(maxLenPalindrome(str, n, ch));
}
}
Python3
def isPalindrome(str, i, j):
while (i < j):
if (str[i] != str[j]):
return False;
i+=1;
j-=1;
return True;
def maxLenPalindrome(str, n, ch):
maxLen = 0;
for i in range(n):
if (str[i] == ch):
for j in range(n-1,i+1,-1):
if (str[j] == ch):
if (isPalindrome(str, i, j)):
maxLen = max(maxLen, j - i + 1);
break;
return maxLen;
str = "lapqooqpqpl";
n = len(str);
ch = 'p';
print(maxLenPalindrome(str, n, ch));
C#
using System;
class GFG
{
static bool isPalindrome(string str,
int i, int j)
{
while (i < j)
{
if (str[i] != str[j])
{
return false;
}
i++;
j--;
}
return true;
}
static int maxLenPalindrome(string str, int n, char ch)
{
int maxLen = 0;
for (int i = 0; i < n; i++)
{
if (str[i] == ch)
{
for (int j = n - 1; j >= i; j--)
{
if (str[j] == ch)
{
if (isPalindrome(str, i, j))
{
maxLen = Math.Max(maxLen, j - i + 1);
break;
}
}
}
}
}
return maxLen;
}
public static void Main()
{
string str = "lapqooqpqpl";
int n = str.Length;
char ch = 'p';
Console.WriteLine(maxLenPalindrome(str, n, ch));
}
}
Javascript
<script>
function isPalindrome(str, i, j) {
while (i < j) {
if (str[i] !== str[j]) {
return false;
}
i++;
j--;
}
return true;
}
function maxLenPalindrome(str, n, ch) {
var maxLen = 0;
for (var i = 0; i < n; i++) {
if (str[i] === ch) {
for (var j = n - 1; j >= i; j--) {
if (str[j] === ch) {
if (isPalindrome(str, i, j)) {
maxLen = Math.max(maxLen, j - i + 1);
break;
}
}
}
}
}
return maxLen;
}
var str = "lapqooqpqpl";
var n = str.length;
var ch = "p";
document.write(maxLenPalindrome(str, n, ch));
</script>
Time Complexity: O(n3 )
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...