Given a string S consisting of N lowercase alphabets, the task is to modify the string S by replacing each character with the alphabet whose circular distance from the character is equal to the frequency of the character in S.
Examples:
Input: S = “geeks”
Output: hgglt
Explanation:
The following modifications are done on the string S:
- The frequency of ‘g’ in the string is 1. Therefore, ‘g’ is replaced by ‘h’.
- The frequency of ‘e’ in the string is 2. Therefore, ‘e’ is replaced by ‘g’.
- The frequency of ‘e’ in the string is 2. Therefore, ‘e’ is replaced by ‘g’.
- The frequency of ‘k’ in the string is 1. Therefore, ‘k’ is converted to ‘k’ + 1 = ‘l’.
- The frequency of ‘s’ in the string is 1. Therefore, ‘s’ is converted to ‘s’ + 1 = ‘t’.
Therefore, the modified string S is “hgglt”.
Input: S = “jazz”
Output: “kbbb”
Approach: The given problem can be solved by maintaining the frequency array that stores the occurrences of each character in the string. Follow the steps below to solve the problem:
- Initialize an array, freq[26] initially with all elements as 0 to store the frequency of each character of the string.
- Traverse the given string S and increment the frequency of each character S[i] by 1in the array freq[].
- Traverse the string S used the variable i and performed the following steps:
- Store the value to be added to S[i] in a variable, add as (freq[i] % 26).
- If, after adding the value of add to S[i], S[i] does not exceed the character z, then update S[i] to S[i] + add.
- Otherwise, update the value of add to (S[i] + add – z) and then set S[i] to (a + add – 1).
- After completing the above steps, print the modified string S.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void addFrequencyToCharacter(string s)
{
int frequency[26] = { 0 };
int n = s.size();
for ( int i = 0; i < n; i++) {
frequency[s[i] - 'a' ] += 1;
}
for ( int i = 0; i < n; i++) {
int add = frequency[s[i] - 'a' ] % 26;
if ( int (s[i]) + add <= int ( 'z' ))
s[i] = char ( int (s[i]) + add);
else {
add = ( int (s[i]) + add) - ( int ( 'z' ));
s[i] = char ( int ( 'a' ) + add - 1);
}
}
cout << s;
}
int main()
{
string str = "geeks" ;
addFrequencyToCharacter(str);
return 0;
}
|
Java
class GFG{
static void addFrequencyToCharacter( char [] s)
{
int frequency[] = new int [ 26 ];
int n = s.length;
for ( int i = 0 ; i < n; i++)
{
frequency[s[i] - 'a' ] += 1 ;
}
for ( int i = 0 ; i < n; i++)
{
int add = frequency[s[i] - 'a' ] % 26 ;
if (( int )(s[i]) + add <= ( int )( 'z' ))
s[i] = ( char )(( int )(s[i]) + add);
else
{
add = (( int )(s[i]) + add) - (( int )( 'z' ));
s[i] = ( char )(( int )( 'a' ) + add - 1 );
}
}
System.out.println(s);
}
public static void main(String[] args)
{
String str = "geeks" ;
addFrequencyToCharacter(str.toCharArray());
}
}
|
Python3
def addFrequencyToCharacter(s):
frequency = [ 0 ] * 26
n = len (s)
for i in range (n):
frequency[ ord (s[i]) - ord ( 'a' )] + = 1
for i in range (n):
add = frequency[ ord (s[i]) - ord ( 'a' )] % 26
if ( ord (s[i]) + add < = ord ( 'z' )):
s[i] = chr ( ord (s[i]) + add)
else :
add = ( ord (s[i]) + add) - ( ord ( 'z' ))
s[i] = chr ( ord ( 'a' ) + add - 1 )
print ("".join(s))
if __name__ = = '__main__' :
str = "geeks"
addFrequencyToCharacter([i for i in str ])
|
C#
using System;
class GFG{
static void addFrequencyToCharacter( char [] s)
{
int [] frequency = new int [26];
int n = s.Length;
for ( int i = 0; i < n; i++)
{
frequency[s[i] - 'a' ] += 1;
}
for ( int i = 0; i < n; i++)
{
int add = frequency[s[i] - 'a' ] % 26;
if (( int )(s[i]) + add <= ( int )( 'z' ))
s[i] = ( char )(( int )(s[i]) + add);
else
{
add = (( int )(s[i]) + add) - (( int )( 'z' ));
s[i] = ( char )(( int )( 'a' ) + add - 1);
}
}
Console.WriteLine(s);
}
public static void Main( string [] args)
{
string str = "geeks" ;
addFrequencyToCharacter(str.ToCharArray());
}
}
|
Javascript
<script>
function addFrequencyToCharacter(s) {
var frequency = new Array(26).fill(0);
var n = s.length;
for ( var i = 0; i < n; i++) {
frequency[s[i].charCodeAt(0) - "a" .charCodeAt(0)] += 1;
}
for ( var i = 0; i < n; i++) {
var add = frequency[s[i].charCodeAt(0) - "a" .charCodeAt(0)] % 26;
if (s[i].charCodeAt(0) + add <= "z" .charCodeAt(0))
s[i] = String.fromCharCode(s[i].charCodeAt(0) + add);
else {
add = s[i].charCodeAt(0) + add - "z" .charCodeAt(0);
s[i] = String.fromCharCode( "a" .charCodeAt(0) + add - 1);
}
}
document.write(s.join( "" ) + "<br>" );
}
var str = "geeks" ;
addFrequencyToCharacter(str.split( "" ));
</script>
|
Time Complexity: O(N) //since there is only one loop to carry out the operations the overall time complexity turns out to be O(N)
Auxiliary Space: O(1) //since there is no extra array or data structure used, it takes constant space.