📜  加密字符串- 2

📅  最后修改于: 2022-05-13 01:56:04.794000             🧑  作者: Mango

加密字符串- 2

给定一个由N个小写英文字母组成的字符串S ,还给定一个字符串是通过首先将由相同字符组成的字符串的每个子字符串替换为该字符的连接和大小的十六进制表示来加密的。子串然后反转整个字符串,任务是找到加密的字符串。

注意:所有十六进制字母都应转换为小写字母。

例子:

方法:可以通过遍历字符串S的字符来解决这个问题。按照以下步骤解决这个问题:

  • 初始化一个空字符串 说, ans来存储答案。
  • 使用变量i遍历字符串S的字符,并执行以下步骤:
    • 从索引i开始查找具有相同字符S[i]的子字符串的计数,并将其存储在变量中,例如count
    • 现在将计数转换为十六进制表示,并附加字符S[i]及其频率十六进制表示。
  • 最后,将字符串ans反转,然后打印出来。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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 = ""
 
    # Iterate the characters
    # of the string
    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
 
        # 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 = "abc"
    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


输出:
1c1b1a

时间复杂度: O(N)
辅助空间: O(N)