Given a string S consisting of N, lower case English alphabet, it is also given that a string is encrypted by first replacing every substring of the string consisting of the same character with the concatenation of that character and the hexadecimal representation of the size of the substring and then revering the whole string, the task is to find the encrypted string.
Note: All Hexadecimal letters should be converted to Lowercase letters.
Examples:
Input: S = “aaaaaaaaaaa”
Output: ba
Explanation:
- First convert the given string to “a11” i.e. write, character along with its frequency.
- Then, change “a11” to “ab” because 11 is b in hexadecimal.
- Then, finally reverse the string i.e “ba”.
Input: S = “abc”
Output: 1c1b1a
Approach: The problem can be solved by iterating over the characters of the string S. Follow the steps below to solve this problem:
Below is the implementation of the above approach:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * convertToHex( int num)
{
char * temp = ( char *) malloc (
sizeof ( char )
* 10);
int i = 0;
while (num != 0) {
int rem = num % 16;
char c;
if (rem < 10) {
c = rem + 48;
}
else {
c = rem + 87;
}
temp[i++] = c;
num = num / 16;
}
if (i == 0)
{
temp[i++] = '0' ;
}
temp[i] = '\0' ;
int j, k = strlen (temp) - 1;
for (j = 0; j < k; j++, k--) {
char t = temp[j];
temp[j] = temp[k];
temp[k] = t;
}
return temp;
}
char * encryptString( char * S, int N)
{
char * ans = ( char *) malloc (
sizeof ( char )
* (N * 3));
ans[0] = '\0' ;
for ( int i = 0; i < N; i++) {
char ch = S[i];
int count = 0;
char * hex;
while (i < N && S[i] == ch) {
count++;
i++;
}
i--;
hex = convertToHex(count);
int len = strlen (ans);
ans[len++] = ch;
ans[len] = '\0' ;
strcat (ans, hex);
}
int i, j, k = strlen (ans) - 1;
for (i = 0, j = k; i < j; i++, j--) {
char t = ans[i];
ans[i] = ans[j];
ans[j] = t;
}
return ans;
}
int main()
{
char S[] = "abc" ;
int N = strlen (S);
char * result = encryptString(S, N);
printf ( "%s" , result);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
string convertToHex( int num)
{
string temp = "" ;
while (num != 0) {
int rem = num % 16;
char c;
if (rem < 10) {
c = rem + 48;
}
else {
c = rem + 87;
}
temp += c;
num = num / 16;
}
return temp;
}
string encryptString(string S, int N)
{
string ans = "" ;
for ( int i = 0; i < N; i++) {
char ch = S[i];
int count = 0;
string hex;
while (i < N && S[i] == ch) {
count++;
i++;
}
i--;
hex = convertToHex(count);
ans += ch;
ans += hex;
}
reverse(ans.begin(), ans.end());
return ans;
}
int main()
{
string S = "abc" ;
int N = S.size();
cout << encryptString(S, N);
return 0;
}
|
Java
import java.awt.*;
import java.util.*;
class GFG
{
static String convertToHex( int num)
{
StringBuilder temp = new StringBuilder();
while (num != 0 ) {
int rem = num % 16 ;
char c;
if (rem < 10 ) {
c = ( char ) (rem + 48 );
}
else {
c = ( char ) (rem + 87 );
}
temp.append(c);
num = num / 16 ;
}
return temp.toString();
}
static String encryptString(String S, int N)
{
StringBuilder ans = new StringBuilder();
for ( int i = 0 ; i < N; i++) {
char ch = S.charAt(i);
int count = 0 ;
String hex;
while (i < N && S.charAt(i) == ch) {
count++;
i++;
}
i--;
hex = convertToHex(count);
ans.append(ch);
ans.append(hex);
}
ans.reverse();
return ans.toString();
}
public static void main(String[] args)
{
String S = "abc" ;
int N = S.length();
System.out.println(encryptString(S, N));
}
}
|
Python3
def convertToHex(num):
temp = ""
while (num ! = 0 ):
rem = num % 16
c = 0
if (rem < 10 ):
c = rem + 48
else :
c = rem + 87
temp + = chr (c)
num = num / / 16
return temp
def encryptString(S, N):
ans = ""
i = 0
while (i<N):
ch = S[i]
count = 0
while (i < N and S[i] = = ch):
count + = 1
i + = 1
hex = convertToHex(count)
ans + = ch
ans + = hex
ans = ans[:: - 1 ]
return ans
if __name__ = = '__main__' :
S = "aaaaaaaaaaa"
N = len (S)
print (encryptString(S, N))
|
C#
using System;
class GFG
{
static string convertToHex( int num)
{
string temp = "" ;
while (num != 0) {
int rem = num % 16;
char c;
if (rem < 10) {
c = ( char ) (rem + 48);
}
else {
c = ( char ) (rem + 87);
}
temp = temp + c;
num = num / 16;
}
return temp;
}
static string encryptString( string S, int N)
{
string ans = "" ;
for ( int i = 0; i < N; i++) {
char ch = S[i];
int count = 0;
string hex;
while (i < N && S[i] == ch) {
count++;
i++;
}
i--;
hex = convertToHex(count);
ans = ans + ch;
ans = ans + hex;
}
char [] Ans = ans.ToCharArray();
Array.Reverse(Ans);
ans = new string (Ans);
return ans;
}
static void Main ()
{
string S = "abc" ;
int N = S.Length;
Console.WriteLine(encryptString(S, N));
}
}
|
Javascript
<script>
function convertToHex(num) {
let temp = "" ;
while (num != 0) {
let rem = num % 16;
let c = 0;
if (rem < 10) {
c = rem + 48;
}
else {
c = rem + 87;
}
temp += String.fromCharCode(c);
num = Math.floor(num / 16);
}
return temp;
}
function encryptString(S, N) {
let ans = "" ;
for (let i = 0; i < N; i++) {
let ch = S[i];
let count = 0;
let hex;
while (i < N && S[i] == ch) {
count++;
i++;
}
i--;
hex = convertToHex(count);
ans += ch;
ans += hex;
}
ans = ans.split( '' ).reverse().join( "" );
return ans;
}
let S = "abc" ;
let N = S.length;
document.write(encryptString(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
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!
Last Updated :
26 Apr, 2023
Like Article
Save Article