Modify a string by circularly shifting each character to the right by respective frequencies


In this problem, we need to right-shift each character of a given string by its frequency. To solve the problem, we can count the frequency of each character and store it in a data structure like an array or map. After that, we can use the ASCII values of the characters to right-shift each character by their frequency.

Problem statement- We have given string str containing lowercase characters and a length equal to N. We need to right-shift each character of the string by the frequency of that particular character in the given string.

Sample examples

Input – str = ‘tutorialspoint’

Output – wvwqskbmtqqkow

Explanation

  • The frequency of ‘t’ is 3. So, ‘t’ + 3 = w.

  • The frequency of ‘u’ is 1. So, ‘u’ + 1 = v

  • The frequency of ‘o’ is 2. So, ‘o’ + 2 = q.

  • The frequency of ‘r’ is 1. So, ‘r’ + 1 = s

  • The frequency of ‘i’ is 2. So, ‘i’ + 2 = k.

  • The frequency of ‘a’ is 1. So, ‘a’ + 1 = b.

  • The frequency of ‘l’ is 1. So, ‘l’ + 1 = m

  • The frequency of ‘s’ is 1. So, ‘s’ + 1 = t.

  • The frequency of ‘p’ is 1. So, ‘p’ + 1 = q.

  • The frequency of ‘n’ is 1. So, ‘n’ + 1 = o.

Input – str = ‘xxxxyyyzz’

Output – ‘bbbbbbbbb’

Explanation

  • The frequency of ‘x’ is 4. So, ‘x’ + 4 = b, as we need to do a circular shift.

  • The frequency of ‘y’ is 3. So, ‘y’ + 3 = b.

  • The frequency of ‘z’ is 4. So, ‘z’ + 2 = b.

Approach 1

In this approach, we will use a map data structure to count and store the frequency of each character in the given string. After that, we can traverse the string and perform a circular shift with each character by its frequency.

Algorithm

  • Define the ‘mp’ map to store the char -> frequency

  • Define the ‘len’ variable and store the length of the string using the length() method.

  • Use the for loop to traverse the string. In the loop, update the frequency of the character by adding 1 to the current value.

  • Now, again, traverse each character of the string.

  • In the loop, access the frequency of the current character and perform the modulo operation with 26. Store the result in the ‘freq’ variable. Here, we need to perform the right shift operation of the character by the ‘freq’ value as if we right shift the character by 26, we get the same

  • If the ASCII value of str[i] + freq is less than the ASCII value of ‘z’, update the str[i] character by adding the ‘freq’ value

  • Else, add the ASCII value of the str[i] to ‘freq’ and subtract the ASCII value of ‘z’. After that, add the resultant value to str[i].

  • Return the final string

Example

#include <iostream>
#include <map> 
//#include <set>
using namespace std;
// Function to shift each character of the string by its frequency.
string shiftCharByFrequancy(string str){
   // map to store the frequency of each character
   map<char, int> mp;
   int len = str.length();
   // Iterate over the string
   for (int i = 0; i < len; i++){
      // update the frequency of the current character
       mp[str[i]] += 1;
   }
   // Traverse the string str
   for (int i = 0; i < len; i++){
      // get the frequency of the current character
      int freq = mp[str[i]] % 26;
      // If the ASCII value of str[i] + freq is less than or equal to the ASCII value of 'z', then update the character by adding freq.
      if (str[i] + freq <= 'z'){
         str[i] = char(int(str[i]) + freq);
      } else {
         // else subtract ASCII value of 'z' from str[i] + freq and add it to ASCII value of 'a' - 1 to get the updated character.
         freq = freq - 'z' + str[i];
         str[i] = char(int('a') + freq - 1);
      }
   }
   return str;
}
int main(){
   string str = "tutorialspoint";
   cout << "The resultant string after circularly right shifting each character of string by its frequency is " << shiftCharByFrequancy(str);
   return 0;
}

Output

The resultant string after circularly right shifting each character of string by its frequency is wvwqskbmtqqkow

Time complexity – O(N), as we traverse the string of length N.

Space complexity – (26) ~ O(1), as we use the map to store the frequency of characters.

We learned to right-shift each character by their frequency in this tutorial. We used the map to store the frequency, and users can use the array or vector to store the frequency. Also, the count() method can be used to count the frequency of a particular character.

Updated on: 10-Aug-2023

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements