Minimum insertions to form a palindrome with permutations allowed Last Updated : 06 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Given a string of lowercase letters. Find minimum characters to be inserted in the string so that it can become palindrome. We can change the positions of characters in the string.Examples: Input: geeksforgeeksOutput: 2Explanation: geeksforgeeks can be changed as: geeksroforskeeg or geeksorfroskeeg and many more using two insertion.Input: aabbcOutput: 0Explanation: aabbc can be changed as: abcba or bacab without any insertionTable of Content[Expected Approach] Character Frequency Counting - O(n) Time and O(1) Space[Alternate Approach] using Bit manipulation - O(n) Time and O(1) Space[Expected Approach] Character Frequency Counting - O(n) Time and O(1) SpaceTo make a string palindromic, count the occurrences of each character. A palindrome can have at most one character with an odd frequency; the rest must occur an even number of times. The minimum insertions required are one less than the count of characters with odd frequencies. If the string is already a palindrome, no insertions are needed (result = 0). C++ #include <bits/stdc++.h> using namespace std; int minInsertion(string &str) { int n = str.length(); int res = 0; vector<int> count(26,0); for (int i = 0; i < n; i++) count[str[i] - 'a']++; for (int i = 0; i < 26; i++) if (count[i] % 2 == 1) res++; return (res == 0) ? 0 : res - 1; } // Driver program int main() { string str = "geeksforgeeks"; cout << minInsertion(str); return 0; } Java public class GfG { static int minInsertion(String str) { int n = str.length(); int res = 0; int[] count = new int[26]; for (int i = 0; i < n; i++) count[str.charAt(i) - 'a']++; for (int i = 0; i < 26; i++) { if (count[i] % 2 == 1) res++; } return (res == 0) ? 0 : res - 1; } // Driver program public static void main(String[] args) { String str = "geeksforgeeks"; System.out.println(minInsertion(str)); } } Python import math as mt def minInsertion(tr1): n = len(str1) res = 0 count = [0 for i in range(26)] for i in range(n): count[ord(str1[i]) - ord('a')] += 1 for i in range(26): if (count[i] % 2 == 1): res += 1 if (res == 0): return 0 else: return res - 1 # Driver Code str1 = "geeksforgeeks" print(minInsertion(str1)) C# using System; public class GfG { static int minInsertion(String str) { int n = str.Length; int res = 0; int[] count = new int[26]; for (int i = 0; i < n; i++) count[str[i] - 'a']++; for (int i = 0; i < 26; i++) { if (count[i] % 2 == 1) res++; } return (res == 0) ? 0 : res - 1; } // Driver program public static void Main() { string str = "geeksforgeeks"; Console.WriteLine(minInsertion(str)); } } JavaScript function minInsertion(str) { let n = str.length; let res = 0; let count = new Array(26); for (let i = 0; i < count.length; i++) { count[i] = 0; } for (let i = 0; i < n; i++) count[str[i].charCodeAt(0) - "a".charCodeAt(0)]++; for (let i = 0; i < 26; i++) { if (count[i] % 2 == 1) res++; } return (res == 0) ? 0 : res - 1; } let str = "geeksforgeeks"; console.log(minInsertion(str)); Output2[Alternate Approach] using Bit manipulation - O(n) Time and O(1) SpaceInitialize a mask to 0 and toggle bits for each character based on its position in the alphabet. If the mask becomes 0, the string is already a palindrome (return 0). Otherwise, the minimum insertions required are the number of set bits in the mask minus one. C++ #include <bits/stdc++.h> using namespace std; int minInsertion(string &str) { long long mask = 0; for (auto c : str) mask ^= (1 << (c - 'a')); if (mask == 0) return 0; int count = 0; while (mask) { count += mask & 1; mask = mask >> 1; } return count - 1; } // Driver program int main() { string str = "geeksforgeeks"; cout << minInsertion(str); return 0; } // This code is contributed by hkdass001 Java import java.util.*; public class GfG { static int minInsertion(String str) { long mask = 0; for (char c : str.toCharArray()) mask ^= (1 << (c - 'a')); if (mask == 0) return 0; int count = 0; while (mask != 0) { count += mask & 1; mask = mask >> 1; } return count - 1; } // Driver program public static void main(String[] args) { String str = "geeksforgeeks"; System.out.println(minInsertion(str)); } } // This code is contributed by Karandeep1234 Python def minInsertion(str): mask = 0 for c in str: mask ^= (1 << (ord(c) - ord('a'))) if mask == 0: return 0 count = 0 while mask: count += mask & 1 mask = mask >> 1 return count - 1 str = "geeksforgeeks" print(minInsertion(str)) C# using System; using System.Collections.Generic; class GfG { static int minInsertion(string str) { int mask = 0; foreach(char c in str) mask ^= (1 << (c - 'a')); if (mask == 0) return 0; int count = 0; while (mask>0) { count += mask & 1; mask = mask >> 1; } return count - 1; } // Driver program static void Main(string[] args) { string str = "geeksforgeeks"; Console.Write(minInsertion(str)); } } JavaScript function minInsertion(str) { let mask = 0; for (let c of str) { mask ^= (1 << (c.charCodeAt(0) - 'a'.charCodeAt(0))); } if (mask === 0) { return 0; } let count = 0; while (mask) { count += mask & 1; mask = mask >> 1; } // return the count minus 1 return count - 1; } // Driver program let str = "geeksforgeeks"; console.log(minInsertion(str)); Output2 Comment More infoAdvertise with us Next Article Minimum insertions to form a palindrome with permutations allowed N nuclode Improve Article Tags : Strings Hash DSA palindrome Practice Tags : HashpalindromeStrings Similar Reads Palindrome String Coding Problems A string is called a palindrome if the reverse of the string is the same as the original one.Example: âmadamâ, âracecarâ, â12321â.Palindrome StringProperties of a Palindrome String:A palindrome string has some properties which are mentioned below:A palindrome string has a symmetric structure which m 2 min read Palindrome String Given a string s, the task is to check if it is palindrome or not.Example:Input: s = "abba"Output: 1Explanation: s is a palindromeInput: s = "abc" Output: 0Explanation: s is not a palindromeUsing Two-Pointers - O(n) time and O(1) spaceThe idea is to keep two pointers, one at the beginning (left) and 13 min read Check Palindrome by Different LanguageProgram to Check Palindrome Number in CPalindrome numbers are those numbers that remain the same even after reversing the order of their digits. In this article, we will learn how to check whether the given number is a palindrome number using C program.ExamplesInput: 121Output: YesExplanation: The number 121 remains the same when its dig 3 min read C Program to Check for Palindrome StringA string is said to be palindrome if the reverse of the string is the same as the string. In this article, we will learn how to check whether the given string is palindrome or not using C program.The simplest method to check for palindrome string is to reverse the given string and store it in a temp 4 min read C++ Program to Check if a Given String is Palindrome or NotA string is said to be palindrome if the reverse of the string is the same as the original string. In this article, we will check whether the given string is palindrome or not in C++.ExamplesInput: str = "ABCDCBA"Output: "ABCDCBA" is palindromeExplanation: Reverse of the string str is "ABCDCBA". So, 4 min read Java Program to Check Whether a String is a PalindromeA string in Java can be called a palindrome if we read it from forward or backward, it appears the same or in other words, we can say if we reverse a string and it is identical to the original string for example we have a string s = "jahaj " and when we reverse it s = "jahaj"(reversed) so they look 8 min read Easy Problems on PalindromeSentence Palindrome Given a sentence s, the task is to check if it is a palindrome sentence or not. A palindrome sentence is a sequence of characters, such as a word, phrase, or series of symbols, that reads the same backward as forward after converting all uppercase letters to lowercase and removing all non-alphanumer 9 min read Check if actual binary representation of a number is palindrome Given a non-negative integer n. The problem is to check if binary representation of n is palindrome or not. Note that the actual binary representation of the number is being considered for palindrome checking, no leading 0âs are being considered. Examples : Input : 9 Output : Yes (9)10 = (1001)2 Inp 6 min read Print longest palindrome word in a sentence Given a string str, the task is to print longest palindrome word present in the string str.Examples: Input : Madam Arora teaches Malayalam Output: Malayalam Explanation: The string contains three palindrome words (i.e., Madam, Arora, Malayalam) but the length of Malayalam is greater than the other t 14 min read Count palindrome words in a sentence Given a string str and the task is to count palindrome words present in the string str. Examples: Input : Madam Arora teaches malayalam Output : 3 The string contains three palindrome words (i.e., Madam, Arora, malayalam) so the count is three. Input : Nitin speaks malayalam Output : 2 The string co 5 min read Check if characters of a given string can be rearranged to form a palindrome Given a string, Check if the characters of the given string can be rearranged to form a palindrome. For example characters of "geeksogeeks" can be rearranged to form a palindrome "geeksoskeeg", but characters of "geeksforgeeks" cannot be rearranged to form a palindrome. Recommended PracticeAnagram P 14 min read Lexicographically first palindromic string Rearrange the characters of the given string to form a lexicographically first palindromic string. If no such string exists display message "no palindromic string". Examples: Input : malayalam Output : aalmymlaa Input : apple Output : no palindromic string Simple Approach: 1. Sort the string charact 13 min read Like