Given a string s we need to tell minimum characters to be appended (insertion at end) to make a string palindrome.
Input : s = "abede" Output : 2 We can make string palindrome as "abedeba" by adding ba at the end of the string. Input : s = "aabb" Output : 2 We can make string palindrome as"aabbaa" by adding aa at the end of the string.
The solution can be achieved by removing characters from the beginning of the string one by one and checking if the string is palindrome or not.
For Example, consider the above string, s = “abede”.
We check if the string is palindrome or not.
The result is false, then we remove the character from the beginning of string and now string becomes “bede”.
We check if the string is palindrome or not. The result is again false, then we remove the character from the beginning of string and now string becomes “ede”.
We check if the string is palindrome or not. The result is true, so the output becomes 2 which is the number of characters removed from the string.
The above approach described and O(n**2) approach.
We also have an algorithm taking the help of Knuth Morris Pratt Algorithm which is O(n) Time Complexity.
The basic idea behind the approach is that we calculate the largest substring from the end can be calculated and the length of the string minus this value is the minimum number of appends. The logic is intuitive, we need not append the palindrome and only those which do not form the palindrome. To find this largest palindrome from the end, we reverse the string, calculate the dfa and reverse the string again(thus gaining back the original string) and finding the final state, which represents the number of matches of the string with the revered string and hence we get the largest substring that is a palindrome from the end, in O(n) time.
Below is the implementation of the above approach:
Suggestion by: <strong> Pratik Priyadarsan </strong>
Related Article :
Dynamic Programming | Set 28 (Minimum insertions to form a palindrome)
This article is contributed by Shubham Chaudhary. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Minimum number of bracket reversals needed to make an expression balanced
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum number of letters needed to make a total of n
- Minimum minutes needed to make the time palindromic
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum number of characters to be replaced to make a given string Palindrome
- Minimum number of deletions to make a string palindrome
- Minimum number of steps needed to remove the substring K from given string
- Minimum characters to be added at front to make string palindrome
- Count minimum swap to make string palindrome
- Number of Counterclockwise shifts to make a string palindrome
- Minimum removal to make palindrome permutation
- Minimum changes required to make each path in a matrix palindrome
- Minimum amount of lamps needed to be installed
- Remove a character from a string to make it a palindrome
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Least number of manipulations needed to ensure two strings have identical characters
- Number of closing brackets needed to complete a regular bracket sequence
- Make largest palindrome by changing at most K-digits