📜  使用以下操作加密给定的字符串

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

使用以下操作加密给定的字符串

给定一个字符串s ,任务是按以下方式加密该字符串:

  1. 如果当前字符的频率是偶数,则将当前字符增加 x。
  2. 如果当前字符的频率是奇数,则将当前字符减 x。

注意:所有操作都是循环的,将 1 加到 'z' 将得到 'a',从 'a' 减去 1 将得到 'z'

例子:

方法:

  • 创建一个频率数组来存储每个字符的频率。
  • 遍历给定的字符串,如果每个字符的频率是偶数,则将其增加 x,否则如果频率为奇数,则将其减少 x。

下面是上述方法的实现:

C++
// C++ implementation of the above approach:
#include 
#define MAX 26
using namespace std;
 
// Function to return the encrypted string
string encryptStr(string str, int n, int x)
{
     
    // Reduce x because rotation of
    // length 26 is unnecessary
    x = x % MAX;
     
    // Calculate the frequency of characters
    int freq[MAX] = {0};
     
    for(int i = 0; i < n; i++)
    {
        freq[str[i] - 'a']++;
    }
     
    for(int i = 0; i < n; i++)
    {
         
        // If the frequency of current character
        // is even then increment it by x
        if (freq[str[i] - 'a'] % 2 == 0)
        {
            int pos = (str[i] - 'a' + x) % MAX;
            str[i] = (char)(pos + 'a');
        }
         
        // Else decrement it by x
        else
        {
            int pos = (str[i] - 'a' - x);
             
            if (pos < 0)
            {
                pos += MAX;
            }
             
            str[i] = (char)(pos + 'a');
        }
    }
     
    // Return the count
    return str;
}
 
// Driver code
int main()
{
    string s = "abcda";
    int n = s.size();
    int x = 3;
     
    cout << encryptStr(s, n, x) << endl;
     
    return 0;
}
 
// This code is contributed by avanitrachhadiya2155


Java
// Java implementation of the above approach:
public class GFG {
 
    static final int MAX = 26;
 
    // Function to return the encrypted string
    static String encryptStr(String str, int n, int x)
    {
 
        // Reduce x because rotation of
        // length 26 is unnecessary
        x = x % MAX;
        char arr[] = str.toCharArray();
 
        // calculate the frequency of characters
        int freq[] = new int[MAX];
        for (int i = 0; i < n; i++)
            freq[arr[i] - 'a']++;
 
        for (int i = 0; i < n; i++) {
 
            // If the frequency of current character
            // is even then increment it by x
            if (freq[arr[i] - 'a'] % 2 == 0) {
                int pos = (arr[i] - 'a' + x) % MAX;
                arr[i] = (char)(pos + 'a');
            }
 
            // Else decrement it by x
            else {
                int pos = (arr[i] - 'a' - x);
                if (pos < 0)
                    pos += MAX;
                arr[i] = (char)(pos + 'a');
            }
        }
 
        // Return the count
        return String.valueOf(arr);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "abcda";
        int n = s.length();
        int x = 3;
        System.out.println(encryptStr(s, n, x));
    }
}


Python3
# Python3 implementation of the above approach:
MAX = 26
 
# Function to return the encrypted string
def encryptstrr(strr, n, x):
     
    # Reduce x because rotation of
    # length 26 is unnecessary
    x = x % MAX
    arr = list(strr)
     
    # calculate the frequency of characters
    freq = [0]*MAX
    for i in range(n):
        freq[ord(arr[i]) - ord('a')] += 1
     
    for i in range(n):
         
        # If the frequency of current character
        # is even then increment it by x
        if (freq[ord(arr[i]) - ord('a')] % 2 == 0):
            pos = (ord(arr[i]) - ord('a') + x) % MAX
            arr[i] = chr(pos + ord('a'))
         
        # Else decrement it by x
        else:
            pos = (ord(arr[i]) - ord('a') - x)
            if (pos < 0):
                pos += MAX
            arr[i] = chr(pos + ord('a'))
             
    # Return the count
    return "".join(arr)
 
 
# Driver code
s = "abcda"
n = len(s)
x = 3
print(encryptstrr(s, n, x))
 
# This code is contributed by
# shubhamsingh10


C#
// C# implementation of the above approach:
using System;
 
class GFG
{
 
    static int MAX = 26;
 
    // Function to return the encrypted string
    public static char[] encryptStr(String str,
                                    int n, int x)
    {
 
        // Reduce x because rotation of
        // length 26 is unnecessary
        x = x % MAX;
        char[] arr = str.ToCharArray();
 
        // calculate the frequency of characters
        int[] freq = new int[MAX];
        for (int i = 0; i < n; i++)
            freq[arr[i] - 'a']++;
 
        for (int i = 0; i < n; i++)
        {
 
            // If the frequency of current character
            // is even then increment it by x
            if (freq[arr[i] - 'a'] % 2 == 0)
            {
                int pos = (arr[i] - 'a' + x) % MAX;
                arr[i] = (char)(pos + 'a');
            }
 
            // Else decrement it by x
            else
            {
                int pos = (arr[i] - 'a' - x);
                if (pos < 0)
                    pos += MAX;
                arr[i] = (char)(pos + 'a');
            }
        }
 
        // Return the count
        return arr;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String s = "abcda";
        int n = s.Length;
        int x = 3;
        Console.WriteLine(encryptStr(s, n, x));
    }
}
 
// This code is contributed by
// sanjeev2552


Javascript


输出:
dyzad

时间复杂度: O(N)