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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Print distinct sorted permutations with duplicates allowed in input
- Print all palindrome permutations of a string
- Rearrange characters to form palindrome if possible
- Check if a string can be rearranged to form special palindrome
- Check if characters of a given string can be rearranged to form a palindrome
- Count pair of strings whose concatenation of substrings form a palindrome
- Check if the characters in a string form a Palindrome in O(1) extra space
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Minimum equal palindromic cuts with rearrangements allowed
- Minimum removal to make palindrome permutation
- Minimum cost to convert string into palindrome
- Count minimum swap to make string palindrome
- Minimum number of deletions to make a string palindrome
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum reduce operations to convert a given string into a palindrome