VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-length-palindromic-substring-such-that-it-starts-and-ends-with-given-char/

⇱ Maximum length palindromic substring such that it starts and ends with given char - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum length palindromic substring such that it starts and ends with given char

Last Updated : 8 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:
"pqooqp" is the maximum length palindromic 
sub-string that starts and ends with 'p'.
Input: str = "geeksforgeeks", ch = 'k' 
Output:
"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: 
 


Output: 
6

 
Time Complexity: O(n3)
Space Complexity: O(1)
Comment