Open In App

Encrypt the string – 2

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. First convert the given string to “a11” i.e. write, character along with its frequency.
  2. Then, change “a11” to “ab” because 11 is b in hexadecimal.
  3. 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>
 
// Function to convert Decimal to Hex
char* convertToHex(int num)
{
    char* temp = (char*)malloc(
        sizeof(char)
        * 10); // Allocate memory for temp string
 
    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) // if input number is 0, return "0"
    {
        temp[i++] = '0';
    }
    temp[i] = '\0';
 
    // Reverse the obtained hexadecimal number
    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;
}
 
// Function to encrypt the string
char* encryptString(char* S, int N)
{
 
    char* ans = (char*)malloc(
        sizeof(char)
        * (N * 3)); // Allocate memory for ans string
    ans[0] = '\0'; // Initialize ans string to empty string
 
    // Iterate the characters of the string
    for (int i = 0; i < N; i++) {
 
        char ch = S[i];
        int count = 0;
        char* hex;
 
        // Iterate until S[i] is equal to ch
        while (i < N && S[i] == ch) {
 
            // Update count and i
            count++;
            i++;
        }
 
        // Decrement i by 1
        i--;
 
        // Convert count to hexadecimal representation
        hex = convertToHex(count);
 
        // Append the character
        int len = strlen(ans);
        ans[len++] = ch;
        ans[len] = '\0';
 
        // Append the characters frequency in hexadecimal
        // representation
        strcat(ans, hex);
    }
 
    // Reverse the obtained answer
    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 required answer
    return ans;
}
 
// Driver Code
int main()
{
 
    // Given Input
    char S[] = "abc";
    int N = strlen(S);
 
    // Function Call
    char* result = encryptString(S, N);
    printf("%s", result);
 
    return 0;
}


C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert Decimal to Hex
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;
}
 
// Function to encrypt the string
string encryptString(string S, int N)
{
 
    string ans = "";
 
    // Iterate the characters
    // of the string
    for (int i = 0; i < N; i++) {
 
        char ch = S[i];
        int count = 0;
        string hex;
 
        // Iterate until S[i] is equal to ch
        while (i < N && S[i] == ch) {
 
            // Update count and i
            count++;
            i++;
        }
 
        // Decrement i by 1
        i--;
 
        // Convert count to hexadecimal
        // representation
        hex = convertToHex(count);
 
        // Append the character
        ans += ch;
 
        // Append the characters frequency
        // in hexadecimal representation
        ans += hex;
    }
 
    // Reverse the obtained answer
    reverse(ans.begin(), ans.end());
 
    // Return required answer
    return ans;
}
 
// Driver Code
int main()
{
 
    // Given Input
    string S = "abc";
    int N = S.size();
 
    // Function Call
    cout << encryptString(S, N);
 
    return 0;
}


Java




// Java program for above approach
import java.awt.*;
import java.util.*;
class GFG
{
 
    // Function to convert Decimal to Hex
    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();
    }
 
    // Function to encrypt the string
    static String encryptString(String S, int N)
    {
 
        StringBuilder ans = new StringBuilder();
 
        // Iterate the characters
        // of the string
        for (int i = 0; i < N; i++) {
 
            char ch = S.charAt(i);
            int count = 0;
            String hex;
 
            // Iterate until S[i] is equal to ch
            while (i < N && S.charAt(i) == ch) {
 
                // Update count and i
                count++;
                i++;
            }
 
            // Decrement i by 1
            i--;
 
            // Convert count to hexadecimal
            // representation
            hex = convertToHex(count);
 
            // Append the character
            ans.append(ch);
 
            // Append the characters frequency
            // in hexadecimal representation
            ans.append(hex);
        }
 
        // Reverse the obtained answer
        ans.reverse();
 
        // Return required answer
        return ans.toString();
    }
     
    // Driver Code
    public static void main(String[] args)
    {
       
        // Given Input
        String S = "abc";
        int N = S.length();
 
        // Function Call
        System.out.println(encryptString(S, N));
    }
}
 
// This code is contributed by hritikrommie.


Python3




# Python3 program for the above approach
 
# Function to convert Decimal to Hex
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
 
# Function to encrypt the string
def encryptString(S, N):
 
    ans = ""
    i = 0
 
    # Iterate the characters
    # of the string
    while (i<N): #changed from for i in range(N)
        ch = S[i]
        count = 0
 
        # Iterate until S[i] is equal to ch
        while (i < N and S[i] == ch):
 
            # Update count and i
            count += 1
            i += 1
 
        # Decrement i by 1
        #i -= 1 # not required
 
        # Convert count to hexadecimal
        # representation
        hex = convertToHex(count)
 
        # Append the character
        ans += ch
 
        # Append the characters frequency
        # in hexadecimal representation
        ans += hex
 
    # Reverse the obtained answer
    ans = ans[::-1]
     
    # Return required answer
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    S = "aaaaaaaaaaa"
    N = len(S)
 
    # Function Call
    print(encryptString(S, N))
 
# This code is contributed by mohit kumar 29


C#




// C# program for above approach
using System;
class GFG
{
     
    // Function to convert Decimal to Hex
    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;
    }
     
    // Function to encrypt the string
    static string encryptString(string S, int N)
    {
  
        string ans = "";
  
        // Iterate the characters
        // of the string
        for (int i = 0; i < N; i++) {
  
            char ch = S[i];
            int count = 0;
            string hex;
  
            // Iterate until S[i] is equal to ch
            while (i < N && S[i] == ch) {
  
                // Update count and i
                count++;
                i++;
            }
  
            // Decrement i by 1
            i--;
  
            // Convert count to hexadecimal
            // representation
            hex = convertToHex(count);
  
            // Append the character
            ans = ans + ch;
  
            // Append the characters frequency
            // in hexadecimal representation
            ans = ans + hex;
        }
  
        // Reverse the obtained answer
        char[] Ans = ans.ToCharArray();
        Array.Reverse(Ans);
        ans = new string(Ans);
  
        // Return required answer
        return ans;
    }
     
  static void Main ()
  {
    // Given Input
    string S = "abc";
    int N = S.Length;
 
    // Function Call
    Console.WriteLine(encryptString(S, N));
  }
}
 
// This code is contributed by suresh07.


Javascript




<script>
 
// JavaScript program for the above approach
 
 
// Function to convert Decimal to Hex
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 to encrypt the string
function encryptString(S, N) {
 
    let ans = "";
 
    // Iterate the characters
    // of the string
    for (let i = 0; i < N; i++) {
 
        let ch = S[i];
        let count = 0;
        let hex;
 
        // Iterate until S[i] is equal to ch
        while (i < N && S[i] == ch) {
 
            // Update count and i
            count++;
            i++;
        }
 
        // Decrement i by 1
        i--;
 
        // Convert count to hexadecimal
        // representation
        hex = convertToHex(count);
 
        // Append the character
        ans += ch;
 
        // Append the characters frequency
        // in hexadecimal representation
        ans += hex;
    }
 
    // Reverse the obtained answer
    ans = ans.split('').reverse().join("");
 
    // Return required answer
    return ans;
}
 
// Driver Code
 
 
// Given Input
let S = "abc";
let N = S.length;
 
// Function Call
document.write(encryptString(S, N));
 
</script>


Output

1c1b1a

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

 



Last Updated : 26 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads