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 firstname.lastname@example.org. 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.
- Minimum insertions to form a palindrome | DP-28
- Minimum insertions to form shortest palindrome
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- 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
- 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
- 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