Replace minimal number of characters to make all characters pair wise distinct
Last Updated :
05 Sep, 2022
Given a string consisting of only lowercase English alphabets. The task is to find and replace the minimal number of characters with any lower case character in the given string such that all pairs of characters formed from the string are distinct. If it is impossible to do so, print “IMPOSSIBLE”.
Note: If there is more than one answer possible, print the string which is lexicographically smallest.
Examples:
Input : str = “xxxxyyyy”
Output : abcxdefy
In the above example, there are 4 occurrences of x and 4 occurrences of y, so we can change 3 occurrences of x to a, b and c and another 3 occurrences of y to d, e, f so that all of the characters are pairwise distinct.
Input : str = “theanswerforthistestisimpossible”
Output : IMPOSSIBLE
It is impossible to change characters to make all the characters pairwise distinct.
Approach: The problem is based on the frequency of each character present in the string. If the length of the string is greater than 26, then it is always impossible to make the characters pairwise distinct, as there are only 26 lowercase characters in the English alphabet.
If the length of the string is less than or equals 26,
- Make a hash array to store the frequency of all characters in the string.
- Start traversing the string.
- Check if the frequency of the current character in the string is greater than 1.
- If Yes, then traverse the hash and find the first character starting from ‘a’ which has not appeared in the string yet.
- Reduce the frequency of the current character in the string and replace the current character in the string with the current character in the hash.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string>
using namespace std;
void minReplacement(string str)
{
if (str.length() > 26)
cout << "IMPOSSIBLE" ;
else {
int hash[26] = { 0 };
for ( int i = 0; i < str.length(); i++)
hash[str[i] - 'a' ]++;
int count = 0;
for ( int i = 0; i < str.length(); i++) {
if (hash[str[i] - 'a' ] > 1) {
for ( int j = 0; j < 26; j++) {
if (hash[j] == 0) {
hash[str[i] - 'a' ]--;
str[i] = ( char )(j + 'a' );
hash[j]++;
break ;
}
}
}
}
cout << str;
}
}
int main()
{
string str = "xxxxyyyy" ;
minReplacement(str);
return 0;
}
|
Java
class GFG {
static void minReplacement(String str) {
if (str.length() > 26 ) {
System.out.println( "IMPOSSIBLE" );
} else {
int hash[] = new int [ 26 ];
for ( int i = 0 ; i < str.length(); i++) {
hash[str.charAt(i) - 'a' ]++;
}
int count = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
if (hash[str.charAt(i) - 'a' ] > 1 ) {
for ( int j = 0 ; j < 26 ; j++) {
if (hash[j] == 0 ) {
hash[str.charAt(i) - 'a' ]--;
str = str.substring( 0 , i) + ( char ) (j + 'a' ) + str.substring(i + 1 );
hash[j]++;
break ;
}
}
}
}
System.out.println(str);
}
}
public static void main(String[] args) {
String str = "xxxxyyyy" ;
minReplacement(str);
}
}
|
Python3
def minReplacement(string):
if len (string) > 26 :
print ( "IMPOSSIBLE" )
else :
Hash = [ 0 ] * 26
for i in range ( 0 , len (string)):
Hash [ ord (string[i]) - ord ( 'a' )] + = 1
count = 0
for i in range ( 0 , len (string)):
if Hash [ ord (string[i]) - ord ( 'a' )] > 1 :
for j in range ( 0 , 26 ):
if Hash [j] = = 0 :
Hash [ ord (string[i]) - ord ( 'a' )] - = 1
string[i] = chr (j + ord ( 'a' ))
Hash [j] + = 1
break
print (''.join(string))
if __name__ = = "__main__" :
string = "xxxxyyyy"
minReplacement( list (string))
|
C#
using System;
class GFG
{
static void minReplacement(String str)
{
if (str.Length > 26)
{
Console.WriteLine( "IMPOSSIBLE" );
}
else
{
int []hash = new int [26];
for ( int i = 0; i < str.Length; i++)
{
hash[str[i] - 'a' ]++;
}
for ( int i = 0; i < str.Length; i++)
{
if (hash[str[i] - 'a' ] > 1)
{
for ( int j = 0; j < 26; j++)
{
if (hash[j] == 0)
{
hash[str[i] - 'a' ]--;
str = str.Substring(0, i) +
( char ) (j + 'a' ) +
str.Substring(i + 1);
hash[j]++;
break ;
}
}
}
}
Console.WriteLine(str);
}
}
public static void Main()
{
String str = "xxxxyyyy" ;
minReplacement(str);
}
}
|
PHP
<?php
function minReplacement( $str )
{
if ( strlen ( $str ) > 26)
echo "IMPOSSIBLE" ;
else
{
$hash = array_fill (0, 26, NULL);
for ( $i = 0; $i < strlen ( $str ); $i ++)
$hash [ord( $str [ $i ]) - ord( 'a' )]++;
$count = 0;
for ( $i = 0; $i < strlen ( $str ); $i ++)
{
if ( $hash [ord( $str [ $i ]) - ord( 'a' )] > 1)
{
for ( $j = 0; $j < 26; $j ++)
{
if ( $hash [ $j ] == 0)
{
$hash [ord( $str [ $i ]) - ord( 'a' )]--;
$str [ $i ] = chr ( $j + ord( 'a' ));
$hash [ $j ]++;
break ;
}
}
}
}
echo $str ;
}
}
$str = "xxxxyyyy" ;
minReplacement( $str );
?>
|
Javascript
<script>
function minReplacement(str)
{
if (str.length > 26) {
document.write( "IMPOSSIBLE<br>" );
}
else {
let hash = new Array(26);
for (let i=0;i<26;i++)
{
hash[i]=0;
}
for (let i = 0; i < str.length; i++) {
hash[str[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
let count = 0;
for (let i = 0; i < str.length; i++) {
if (hash[str[i].charCodeAt(0) - 'a' .charCodeAt(0)] > 1) {
for (let j = 0; j < 26; j++) {
if (hash[j] == 0) {
hash[str[i].charCodeAt(0) - 'a' .charCodeAt(0)]--;
str = str.substring(0, i) +
String.fromCharCode(j + 'a' .charCodeAt(0)) +
str.substring(i + 1);
hash[j]++;
break ;
}
}
}
}
document.write(str);
}
}
let str = "xxxxyyyy" ;
minReplacement(str);
</script>
|
Complexity Analysis:
- Time Complexity: O(N), where N is the length of the string
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...