Given a string of lowercase letters. Find minimum characters to be inserted in string so that it can become palindrome. We can change positions of characters in string.
Input : geeksforgeeks Output : 2 geeksforgeeks can be changed as: geeksroforskeeg geeksorfroskeeg and many more Input : aabbc Output : 0 aabbc can be changed as: abcba bacab
A palindromic string can have one odd character only when length of string is odd otherwise all characters occur even number of times. So, we have to find characters which occur odd times in a string.
The idea is to count occurrence of each character in a string. As palindromic string can have one character which occur odd times so number of insertion will be one less then count of characters which occur odd times. And if string is already palindrome, we do not need to add any character so result will be 0.
This article is contributed by nuclode. 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 insertions to form a palindrome | DP-28
- Minimum insertions to form shortest palindrome
- Longest common subsequence with permutations allowed
- Generate permutations with only adjacent swaps allowed
- Print distinct sorted permutations with duplicates allowed in input
- Print all palindrome permutations of a string
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Minimum equal palindromic cuts with rearrangements allowed
- Check if characters of a given string can be rearranged to form a palindrome
- Rearrange characters to form palindrome if possible
- Check if the characters in a string form a Palindrome in O(1) extra space
- Check if a string can be rearranged to form special palindrome
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Count pair of strings whose concatenation of substrings form a palindrome
- Longest substring whose characters can be rearranged to form a Palindrome
- Minimum characters to be added at front to make string palindrome
- Minimum number of Appends needed to make a string palindrome
- Minimum number of deletions to make a string palindrome