Encrypt the string


Encryption is the technique to change the data by using some techniques or certain steps so it changes to another information or the previous information cannot be gathered from it directly. For encryption, we have to follow certain steps that are fixed for a particular type of encryption.

In this problem, we will be given a string and we have to encrypt it by following the given steps −

  • First, we have to get all the substring that contains the same characters and replace that substring with a single character followed by the length of the substring.

  • Now, change the length to the hexadecimal value and all characters of the hexadecimal value must be changed to lowercase.

  • In the end, reverse the complete string.

Sample Examples

Input 1: string str = "aabbbcccc"
Output: "4c3b2a"

Explanation

First, we will get all the substrings that contain the same number of characters and replace them with the frequency of their characters this will give us the string “a2b3c4”. Now we will change the length to the hexadecimal value but 2, 3, and 4 have the same value in the hexadecimal form. At the end we will reverse the string and the final result will be 4c3b2a.

Input2: string str = "oooooooooooo"
Output: "co"

Explanation

First, we will convert the string to the frequency string that will be “o12”. Now, the hexadecimal value of the 12 is C and we will change it to lowercase which is c, and replace it in the string and then reverse the string.

Approach

From the above examples, we got an idea about the problem, now let us move to the implementation part −

  • In the implementation, first, we will implement a function to take an input as the integer and will return a string as the return value.

  • This function will be used to convert the given integer to a hexadecimal value with one modification of using lowercase English letters instead of uppercase English characters.

  • We will define another function in which we will traverse over the string using the for loop and then for the substring of the same characters we will use the while loop until we will find the characters equal to our current character.

  • We will count the frequency and will change that into the hexadecimal value and add it to the string with the current index character.

  • At last, we will reverse the string and return it to print in the main function.

Example

#include <bits/stdc++.h>
using namespace std;
// function to convert the integer to hexadecimal values 
string convertToHexa(int val){
   string res = ""; // string to store the result     
   while(val != 0){
      int cur = val %16; // getting the mode of the current value         
      if(cur < 10){
         res += '0' + cur;
       }
      else{
         res += 87 + cur; // adding 87 to get the lowercase letters 
      }
      val /= 16; // updating the current value 
   }
   return res;
}

// function to encrypt the string 
string encrypt(string str){
   int len = str.length(); // getting the length of the string 
   int freq = 0; // variable to store the frequency 
   string ans = ""; // string to store the answer    
   
   // traversing over the string 
   for(int i=0; i<len; i++){
      int j = i; // variable to keep track the substring with the same character
      while(j < len && str[j] == str[i]){
         j++;
      }
      freq = j-i;
      ans += str[i];
      
      // calling the function to get the hexadecimal value 
      string hexaValue = convertToHexa(freq);
      ans += hexaValue;        
      i = j-1;
   } 
   
   // reversing the string 
   reverse(ans.begin(), ans.end());
   return ans;
}

// main function 
int main(){
   string str = "aaabbbbccccccccccc"; // given string  
   
   // calling the function to get the encrypted string
   cout<<"The given string after the encryption is: "<<encrypt(str)<<endl;
   return 0;
}

Output

The given string after the encryption is: bc4b3a

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given string. We are traversing over the string cost us time N and for the reversal of string it is less as compared to N.

The space complexity of the above code is O(N) to store the final string and if we ignore that then there is no extra space used.

Note

Encryption can be done in an unlimited number of ways and only matters how the rules are defined to encrypt the key. The main thing about encryption is it must give the same result every time for the same input.

Conclusion

In this tutorial, we have implemented a code to encrypt a given string according to the rules, first, we have to get the substrings that contain the same type of elements are replace them with the character and their frequency. In the next step, we have changed the frequency to the hexadecimal number and reversed the whole string at last. The time complexity of the above code is O(N).

Updated on: 26-Jul-2023

652 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements