![]() |
VOOZH | about |
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:
6
Time Complexity: O(n3) Space Complexity: O(1)