Encrypt the string (2024)

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started

Encrypt the string (2)

Advertisem*nts

Encrypt the string (2024)
Top Articles
Required minimum distributions (RMD)
28 Fascinating Real Estate Facts: Prepare to Be Shocked and Educated
Knoxville Tennessee White Pages
Top Scorers Transfermarkt
Kobold Beast Tribe Guide and Rewards
Videos De Mexicanas Calientes
What Auto Parts Stores Are Open
Is Sportsurge Safe and Legal in 2024? Any Alternatives?
Co Parts Mn
New Day Usa Blonde Spokeswoman 2022
Kagtwt
Lqse-2Hdc-D
Dusk
Sport Clip Hours
Craigslist Alabama Montgomery
Reddit Wisconsin Badgers Leaked
Grasons Estate Sales Tucson
Caledonia - a simple love song to Scotland
Pjs Obits
Lakers Game Summary
Quick Answer: When Is The Zellwood Corn Festival - BikeHike
Atdhe Net
Okc Body Rub
Boston Dynamics’ new humanoid moves like no robot you’ve ever seen
What Are The Symptoms Of A Bad Solenoid Pack E4od?
Mandy Rose - WWE News, Rumors, & Updates
Hefkervelt Blog
Jailfunds Send Message
Things to do in Pearl City: Honolulu, HI Travel Guide by 10Best
Winterset Rants And Raves
Otis Inmate Locator
Lawrence Ks Police Scanner
Bursar.okstate.edu
Alima Becker
Gr86 Forums
2012 Street Glide Blue Book Value
Craigslist Boats Eugene Oregon
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Thelemagick Library - The New Comment to Liber AL vel Legis
2023 Fantasy Football Draft Guide: Rankings, cheat sheets and analysis
Live Delta Flight Status - FlightAware
Brandon Spikes Career Earnings
Joey Gentile Lpsg
Exam With A Social Studies Section Crossword
Pink Runtz Strain, The Ultimate Guide
Craigslist Antique
Comanche Or Crow Crossword Clue
Swsnj Warehousing Inc
Star Sessions Snapcamz
Craigslist Yard Sales In Murrells Inlet
Koniec veľkorysých plánov. Prestížna LEAF Academy mení adresu, masívny kampus nepostaví
Códigos SWIFT/BIC para bancos de USA
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 6381

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.