Minimum insertions to form a palindrome with permutations allowed
Last Updated :
15 Sep, 2023
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 : geeksforgeeks
Output : 2
geeksforgeeks can be changed as:
geeksroforskeeg
geeksorfroskeeg
and many more
Input : aabbc
Output : 0
aabbc can be changed as:
abcba
bacab
Method 1: A palindromic string can have one odd character only when the length of the string is odd otherwise all characters occur an even number of times. So, we have to find characters that occur at odd times in a string.
The idea is to count the occurrence of each character in a string. As a palindromic string can have one character which occurs odd times, so the number of insertion will be one less than the count of characters that occur at odd times. And if the string is already palindrome, we do not need to add any character, so the result will be 0.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minInsertion(string str)
{
int n = str.length();
int res = 0;
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;
}
int main()
{
string str = "geeksforgeeks" ;
cout << minInsertion(str);
return 0;
}
|
Java
public class Palindrome {
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 ;
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
System.out.println(minInsertion(str));
}
}
|
Python3
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
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;
}
public static void Main()
{
string str = "geeksforgeeks" ;
Console.WriteLine(minInsertion(str));
}
}
|
PHP
<?php
function minInsertion( $str )
{
$n = strlen ( $str );
$res = 0;
$count = array (26);
for ( $i = 0; $i < $n ; $i ++)
$count [ord( $str [ $i ]) - ord( 'a' )]++;
for ( $i = 0; $i < 26; $i ++)
{
if ( $count [ $i ] % 2 == 1)
$res ++;
}
return ( $res == 0) ? 0 : $res - 1;
}
$str = "geeksforgeeks" ;
echo (minInsertion( $str ));
?>
|
Javascript
<script>
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" ;
document.write(minInsertion(str));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: An approach using Bit manipulation:
- Create a mask and initialise it to zero.
- For each character in string str, toggle the bit into the mask with its corresponding position in the alphabet.
- Check if mask is equal to zero, and return 0.
- Otherwise, return number of setbit in mask – 1.
Below is the implementation of the above approach:
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;
}
int main()
{
string str = "geeksforgeeks" ;
cout << minInsertion(str);
return 0;
}
|
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 ;
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
System.out.println(minInsertion(str));
}
}
|
Python3
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;
}
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 count - 1;
}
let str = "geeksforgeeks" ;
console.log(minInsertion(str));
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...