Given a string S consisting of lowercase English alphabets, the task is to right shift each character of the given string S circularly by its frequency.
Circular shifting of characters refers to shifting character ‘z’ to ‘a’, as its next character.
Examples:
Input: S = “geeksforgeeks”
Output: iiimugpsiiimu
Explanation:
Following changes are made on the string S:
- Frequency of ‘g’ is 2. Therefore, shifting the character ‘g’ by 2 becomes ‘i’.
- Frequency of ‘e’ is 4. Therefore, shifting the character ‘e’ by 4 becomes ‘i’.
- Frequency of ‘k’ is 2. Therefore, shifting the character ‘k’ by 2 becomes ‘m’.
- Frequency of ‘s’ is 2. Therefore, shifting the character ‘s’ by 2 becomes ‘u’.
- Frequency of ‘f’ is 1. Therefore, shifting the character ‘f’ by 1 becomes ‘g’.
- Frequency of ‘o’ is 1. Therefore, shifting the character ‘o’ by 1 becomes ‘p’.
- Frequency of ‘r’ is 1. Therefore, shifting the character ‘r’ by 1 becomes ‘s’.
After the above shifting of characters, the string modifies to “iiimugpsiiimu”.
Input: S = “aabcadb”
Output: ddddded
Approach: The idea to solve this problem is to traverse the string and find the frequency of occurrence of each character in the string and then increment each of the characters by its frequency. Follow the steps below to solve the problem:
- Initialize an array, say frequency[] that stores the occurrences of each character in the string S.
- Traverse the given string S and perform the following steps:
- Find the frequency of the current character S[i].
- Increment the current character by its frequency and update the value of S[i] to its updated character.
- After completing the above steps, print the string S as the resultant modified string.
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.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);
}
}
cout << S;
}
int main()
{
string S = "geeksforgeeks" ;
addFrequencyToCharacter(S);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static void addFrequencyToCharacter(String Str)
{
int frequency[] = new int [ 26 ];
int N = Str.length();
char S[] = Str.toCharArray();
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( new String(S));
}
public static void main(String[] args)
{
String S = "geeksforgeeks" ;
addFrequencyToCharacter(S);
}
}
|
Python3
def addFrequencyToCharacter(S):
frequency = [ 0 for i in range ( 26 )]
N = len (S)
S = list (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 )
s = ""
print (s.join(S))
if __name__ = = '__main__' :
S = "geeksforgeeks"
addFrequencyToCharacter(S)
|
C#
using System;
class GFG{
static void addFrequencyToCharacter( string Str)
{
int [] frequency = new int [26];
int N = Str.Length;
char [] S = Str.ToCharArray();
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.Write( new string (S));
}
public static void Main( string [] args)
{
string S = "geeksforgeeks" ;
addFrequencyToCharacter(S);
}
}
|
Javascript
<script>
function addFrequencyToCharacter(Str)
{
var frequency = Array.from({length: 26}, (_, i) => 0);
var N = Str.length;
var S = Str.split( '' );
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( '' ));
}
var S = "geeksforgeeks" ;
addFrequencyToCharacter(S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(26)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!