Skip to content
Related Articles

Related Articles

Modify a string by circularly shifting each character to the right by respective frequencies
  • Last Updated : 04 May, 2021

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:

  1. Frequency of ‘g’ is 2. Therefore, shifting the character ‘g’ by 2 becomes ‘i’.
  2. Frequency of ‘e’ is 4. Therefore, shifting the character ‘e’ by 4 becomes ‘i’.
  3. Frequency of ‘k’ is 2. Therefore, shifting the character ‘k’ by 2 becomes ‘m’.
  4. Frequency of ‘s’ is 2. Therefore, shifting the character ‘s’ by 2 becomes ‘u’.
  5. Frequency of ‘f’ is 1. Therefore, shifting the character ‘f’ by 1 becomes ‘g’.
  6. Frequency of ‘o’ is 1. Therefore, shifting the character ‘o’ by 1 becomes ‘p’.
  7. 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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to replace the characters
// by its frequency of character in it
void addFrequencyToCharacter(string S)
{
    // Stores frequencies of characters
    // in the string S
    int frequency[26] = { 0 };
 
    int N = S.length();
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
        // Increment the frequency of
        // each character by 1
        frequency[S[i] - 'a'] += 1;
    }
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
        // Find the frequency of
        // the current character
        int add = frequency[S[i] - 'a'] % 26;
 
        // Update the character
        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);
        }
    }
 
    // Print the resultant string
    cout << S;
}
 
// Driver Code
int main()
{
    string S = "geeksforgeeks";
    addFrequencyToCharacter(S);
 
    return 0;
}

Java




// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to replace the characters
    // by its frequency of character in it
    static void addFrequencyToCharacter(String Str)
    {
        // Stores frequencies of characters
        // in the string S
        int frequency[] = new int[26];
 
        int N = Str.length();
        char S[] = Str.toCharArray();
 
        // Traverse the string S
        for (int i = 0; i < N; i++) {
 
            // Increment the frequency of
            // each character by 1
            frequency[S[i] - 'a'] += 1;
        }
 
        // Traverse the string S
        for (int i = 0; i < N; i++) {
 
            // Find the frequency of
            // the current character
            int add = frequency[S[i] - 'a'] % 26;
 
            // Update the character
            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);
            }
        }
 
        // Print the resultant string
        System.out.println(new String(S));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "geeksforgeeks";
        addFrequencyToCharacter(S);
    }
}
 
// This code is contributed by Kingash.
Output: 
iiimugpsiiimu

 

Time Complexity: O(N)
Auxiliary Space: O(26)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :