Convert given string so that it holds only distinct characters
Given a string str consisting of lowercase English alphabets, the task is to convert the string so that it holds only distinct characters. Any character of the string can be replaced by any other lowercase character (the number of replacements must be minimum). Print the modified string.
Examples:
Input: str = “geeksforgeeks”
Output: abcdhforgieks
Input: str = “bbcaacgkkk”
Output: dbefacghik
Approach: If the length of the string > 26 then it won’t be possible for the string to have all distinct characters.
Else, count the occurrence for each of the character of the string and for the characters that appear more than once, replace them with some character that hasn’t appeared in the string yet. Print the modified string in the end.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int nextZero( int i, int occurrences[])
{
while (i < 26)
{
if (occurrences[i] == 0)
return i;
i++;
}
return -1;
}
string getModifiedString(string str)
{
int n = str.length();
if (n > 26)
return "-1" ;
string ch = str;
int i, occurrences[26] = {0};
for (i = 0; i < n; i++)
occurrences[ch[i] - 'a' ]++;
int index = nextZero(0, occurrences);
for (i = 0; i < n; i++)
{
if (occurrences[ch[i] - 'a' ] > 1)
{
occurrences[ch[i] - 'a' ]--;
ch[i] = ( char )( 'a' + index);
occurrences[index] = 1;
index = nextZero(index + 1, occurrences);
}
}
cout << ch << endl;
}
int main()
{
string str = "geeksforgeeks" ;
getModifiedString(str);
}
|
Java
class GFG {
static int nextZero( int i, int occurrences[])
{
while (i < occurrences.length) {
if (occurrences[i] == 0 )
return i;
i++;
}
return - 1 ;
}
static String getModifiedString(String str)
{
int n = str.length();
if (n > 26 )
return "-1" ;
char ch[] = str.toCharArray();
int i, occurrences[] = new int [ 26 ];
for (i = 0 ; i < n; i++)
occurrences[ch[i] - 'a' ]++;
int index = nextZero( 0 , occurrences);
for (i = 0 ; i < n; i++) {
if (occurrences[ch[i] - 'a' ] > 1 ) {
occurrences[ch[i] - 'a' ]--;
ch[i] = ( char )( 'a' + index);
occurrences[index] = 1 ;
index = nextZero(index + 1 , occurrences);
}
}
return String.valueOf(ch);
}
public static void main(String arr[])
{
String str = "geeksforgeeks" ;
System.out.print(getModifiedString(str));
}
}
|
Python3
def nextZero(i, occurrences):
while i < 26 :
if occurrences[i] = = 0 :
return i
i + = 1
return - 1
def getModifiedString( str ):
n = len ( str )
if n > 26 :
return "-1"
ch = str
ch = list (ch)
occurrences = [ 0 ] * 26
for i in range (n):
occurrences[ ord (ch[i]) - ord ( 'a' )] + = 1
index = nextZero( 0 , occurrences)
for i in range (n):
if occurrences[ ord (ch[i]) - ord ( 'a' )] > 1 :
occurrences[ ord (ch[i]) - ord ( 'a' )] - = 1
ch[i] = chr ( ord ( 'a' ) + index)
occurrences[index] = 1
index = nextZero(index + 1 , occurrences)
ch = ''.join(ch)
print (ch)
if __name__ = = "__main__" :
str = "geeksforgeeks"
getModifiedString( str )
|
C#
using System;
class GFG
{
static int nextZero( int i, int []occurrences)
{
while (i < occurrences.Length)
{
if (occurrences[i] == 0)
return i;
i++;
}
return -1;
}
static string getModifiedString( string str)
{
int n = str.Length ;
if (n > 26)
return "-1" ;
char []ch = str.ToCharArray();
int []occurrences = new int [26];
int i ;
for (i = 0; i < n; i++)
occurrences[ch[i] - 'a' ]++;
int index = nextZero(0, occurrences);
for (i = 0; i < n; i++)
{
if (occurrences[ch[i] - 'a' ] > 1)
{
occurrences[ch[i] - 'a' ]--;
ch[i] = ( char )( 'a' + index);
occurrences[index] = 1;
index = nextZero(index + 1, occurrences);
}
}
string s = new string (ch) ;
return s ;
}
public static void Main()
{
string str = "geeksforgeeks" ;
Console.WriteLine(getModifiedString(str));
}
}
|
Javascript
<script>
function nextZero(i, occurrences)
{
while (i < occurrences.length)
{
if (occurrences[i] == 0)
return i;
i++;
}
return -1;
}
function getModifiedString(str)
{
let n = str.length ;
if (n > 26)
return "-1" ;
let ch = str.split( '' );
let occurrences = new Array(26);
occurrences.fill(0);
let i ;
for (i = 0; i < n; i++)
occurrences[ch[i].charCodeAt() - 'a' .charCodeAt()]++;
let index = nextZero(0, occurrences);
for (i = 0; i < n; i++)
{
if (occurrences[ch[i].charCodeAt() - 'a' .charCodeAt()] > 1)
{
occurrences[ch[i].charCodeAt() - 'a '.charCodeAt()]--;
// Replace the character
ch[i] = String.fromCharCode(' a '.charCodeAt() + index);
// Update the new character' s occurrence
occurrences[index] = 1;
index = nextZero(index + 1, occurrences);
}
}
let s = ch.join( "" );
return s ;
}
let str = "geeksforgeeks" ;
document.write(getModifiedString(str));
</script>
|
Time Complexity :- O(N),where N is the length of the string.
Auxiliary Space: O(1)
Last Updated :
27 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...