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.
Input: str = “lapqooqpqpl”, ch = ‘p’
“pqooqp” is the maximum length palindromic
sub-string that starts and ends with ‘p’.
Input: str = “geeksforgeeks”, ch = ‘k’
“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:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum length of substring whose rotation generates a palindromic substring
- Number of strings which starts and ends with same character after rotations
- Maximum length substring having all same characters after k changes
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Maximum length substring with highest frequency in a string
- Count substrings that starts with character X and ends with character Y
- Find if a string starts and ends with another given string
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 1
- Longest Palindromic Substring | Set 2
- Shortest Palindromic Substring
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Longest palindromic string possible after removal of a substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.