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
- Lexicographically largest possible String after removal of K characters
- Maximum removal from array when removal time >= waiting time
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Minimum length of substring whose rotation generates a palindromic substring
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 1
- Longest Palindromic Substring | Set 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Suffix Tree Application 6 - Longest Palindromic Substring
- Maximum length of balanced string after swapping and removal of characters
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Make palindromic string non-palindromic by rearranging its letters
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Minimum size substring to be removed to make a given string palindromic
- Ropes left after every removal of smallest
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.