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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Longest substring that starts with X and ends with Y
- Find if a string starts and ends with another given string
- Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle
- Count substrings that starts with character X and ends with character Y
- Number of strings which starts and ends with same character after rotations
- Build a DFA to accept Binary strings that starts or ends with "01"
- Minimum length of substring whose rotation generates a palindromic substring
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Ways to split string such that each partition starts with distinct character
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Minimum size substring to be removed to make a given string palindromic
- Longest Even Length Substring such that Sum of First and Second Half is same
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 1
- Longest Palindromic Substring | Set 2
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.