📌  相关文章
📜  通过将每个字符向右循环移动相应频率来修改字符串

📅  最后修改于: 2021-09-06 07:31:04             🧑  作者: Mango

给定一个由小写英文字母组成的字符串S ,任务是将给定字符串S 的每个字符循环右移其频率。

例子:

办法:解决这个问题的想法是遍历字符串,找到字符串中的每个字符出现的频率,然后通过它的频率增加每个字符。请按照以下步骤解决问题:

  • 初始化一个数组,比如frequency[] ,它存储字符串S中每个字符的出现次数。
  • 遍历给定的字符串S并执行以下步骤:
    • 查找当前字符S[i]的频率。
    • 按频率增加当前字符并将S[i]的值更新为其更新后的字符。
  • 完成上述步骤后,将字符串S打印为结果修改后的字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to replace the characters
// by its frequency of character in it
void addFrequencyToCharacter(string S)
{
    // Stores frequencies of characters
    // in the string S
    int frequency[26] = { 0 };
 
    int N = S.length();
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
        // Increment the frequency of
        // each character by 1
        frequency[S[i] - 'a'] += 1;
    }
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
        // Find the frequency of
        // the current character
        int add = frequency[S[i] - 'a'] % 26;
 
        // Update the character
        if (int(S[i]) + add
            <= int('z'))
            S[i] = char(int(S[i])
                        + add);
 
        else {
            add = (int(S[i]) + add)
                  - (int('z'));
            S[i] = char(int('a')
                        + add - 1);
        }
    }
 
    // Print the resultant string
    cout << S;
}
 
// Driver Code
int main()
{
    string S = "geeksforgeeks";
    addFrequencyToCharacter(S);
 
    return 0;
}


Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to replace the characters
    // by its frequency of character in it
    static void addFrequencyToCharacter(String Str)
    {
        // Stores frequencies of characters
        // in the string S
        int frequency[] = new int[26];
 
        int N = Str.length();
        char S[] = Str.toCharArray();
 
        // Traverse the string S
        for (int i = 0; i < N; i++) {
 
            // Increment the frequency of
            // each character by 1
            frequency[S[i] - 'a'] += 1;
        }
 
        // Traverse the string S
        for (int i = 0; i < N; i++) {
 
            // Find the frequency of
            // the current character
            int add = frequency[S[i] - 'a'] % 26;
 
            // Update the character
            if ((int)(S[i]) + add <= (int)('z'))
                S[i] = (char)((int)(S[i]) + add);
 
            else {
                add = ((int)(S[i]) + add) - ((int)('z'));
                S[i] = (char)((int)('a') + add - 1);
            }
        }
 
        // Print the resultant string
        System.out.println(new String(S));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "geeksforgeeks";
        addFrequencyToCharacter(S);
    }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
# Function to replace the characters
# by its frequency of character in it
def addFrequencyToCharacter(S):
 
    # Stores frequencies of characters
    # in the string S
    frequency = [0 for i in range(26)]
 
    N = len(S)
     
    S = list(S)
    # Traverse the string S
    for i in range(N):
 
        # Increment the frequency of
        # each character by 1
        frequency[ord(S[i]) - ord('a')] += 1
     
    # Traverse the string S
    for i in range(N):
 
        # Find the frequency of
        # the current character
        add = frequency[ord(S[i]) - ord('a')] % 26
 
        # Update the character
        if ord(S[i]) + add <= ord('z'):
            S[i] = chr(ord(S[i]) + add)
 
        else:
            add = ord(S[i]) + add - ord('z')
            S[i] = chr(ord('a') + add - 1)
     
    # Print the resultant string
    s = ""
    print(s.join(S))
 
# Driver Code
if __name__ == '__main__':
     
    S = "geeksforgeeks"
     
    addFrequencyToCharacter(S)
     
# This code is contributed by jana_sayantan


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to replace the characters
// by its frequency of character in it
static void addFrequencyToCharacter(string Str)
{
     
    // Stores frequencies of characters
    // in the string S
    int[] frequency = new int[26];
 
    int N = Str.Length;
    char[] S = Str.ToCharArray();
 
    // Traverse the string S
    for(int i = 0; i < N; i++)
    {
         
        // Increment the frequency of
        // each character by 1
        frequency[S[i] - 'a'] += 1;
    }
 
    // Traverse the string S
    for(int i = 0; i < N; i++)
    {
         
        // Find the frequency of
        // the current character
        int add = frequency[S[i] - 'a'] % 26;
 
        // Update the character
        if ((int)(S[i]) + add <= (int)('z'))
            S[i] = (char)((int)(S[i]) + add);
 
        else
        {
            add = ((int)(S[i]) + add) - ((int)('z'));
            S[i] = (char)((int)('a') + add - 1);
        }
    }
 
    // Print the resultant string
    Console.Write(new string(S));
}
 
// Driver Code
public static void Main(string[] args)
{
    string S = "geeksforgeeks";
    addFrequencyToCharacter(S);
}
}
 
// This code is contributed by ukasp


Javascript


输出:
iiimugpsiiimu

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live