Given a string str, the task is to find the longest palindromic string that can be obtained from it after removing a substring.
Input: str = “abcdefghiedcba”
Explanation: Removal of substring “fgh” leaves the remaining string palindromic
Input: str = “abba”
Explanation: Removal of substring “” as the given string is already palindromic.
- Find the longest possible pair of substrings A and B from both ends of the given string which are reverse of each other.
- Remove them from the original string.
- Find the longest palindromic substrings from both ends of the remaining string using KMP and consider the substring which is longer.
- Add the strings A and B to beginning and end of this palindromic substring respectively to get the desired output.
Below code is the implementation of the above approach:
Time complexity: O(N)
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 Palindromic Substring using Palindromic Tree | Set 3
- Longest Palindromic Substring | Set 2
- Longest Palindromic Substring | Set 1
- Longest Non-palindromic substring
- Suffix Tree Application 6 - Longest Palindromic Substring
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Find length of longest subsequence of one string which is substring of another string
- Length of longest palindromic sub-string : Recursion
- Print the longest palindromic prefix of a given string
- Longest palindromic String formed using concatenation of given strings in any order
- Longest substring of only 4's from the first N characters of the infinite string
- Longest substring of 0s in a string formed by k concatenations
- Minimum length of substring whose rotation generates a palindromic substring
- Find the longest substring with k unique characters in a given string
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Make palindromic string non-palindromic by rearranging its letters
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.