给定一个由小写英文字母组成的字符串S ,任务是将给定字符串S 的每个字符循环右移其频率。
Circular shifting of characters refers to shifting character ‘z’ to ‘a’, as its next character.
例子:
Input: S = “geeksforgeeks”
Output: iiimugpsiiimu
Explanation:
Following changes are made on the string S:
- Frequency of ‘g’ is 2. Therefore, shifting the character ‘g’ by 2 becomes ‘i’.
- Frequency of ‘e’ is 4. Therefore, shifting the character ‘e’ by 4 becomes ‘i’.
- Frequency of ‘k’ is 2. Therefore, shifting the character ‘k’ by 2 becomes ‘m’.
- Frequency of ‘s’ is 2. Therefore, shifting the character ‘s’ by 2 becomes ‘u’.
- Frequency of ‘f’ is 1. Therefore, shifting the character ‘f’ by 1 becomes ‘g’.
- Frequency of ‘o’ is 1. Therefore, shifting the character ‘o’ by 1 becomes ‘p’.
- Frequency of ‘r’ is 1. Therefore, shifting the character ‘r’ by 1 becomes ‘s’.
After the above shifting of characters, the string modifies to “iiimugpsiiimu”.
Input: S = “aabcadb”
Output: ddddded
办法:解决这个问题的想法是遍历字符串,找到字符串中的每个字符出现的频率,然后通过它的频率增加每个字符。请按照以下步骤解决问题:
- 初始化一个数组,比如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