给定一个字符串S选自N小写字母,该任务是通过用其圆形距离从字符等于S中的字符的频率字母替换每个字符来修改字符串S。
例子:
Input: S = “geeks”
Output: hgglt
Explanation:
The following modifications are done on the string S:
- The frequency of ‘g’ in the string is 1. Therefore, ‘g’ is replaced by ‘h’.
- The frequency of ‘e’ in the string is 2. Therefore, ‘e’ is replaced by ‘g’.
- The frequency of ‘e’ in the string is 2. Therefore, ‘e’ is replaced by ‘g’.
- The frequency of ‘k’ in the string is 1. Therefore, ‘k’ is converted to ‘k’ + 1 = ‘l’.
- The frequency of ‘s’ in the string is 1. Therefore, ‘s’ is converted to ‘s’ + 1 = ‘t’.
Therefore, the modified string S is “hgglt”.
Input: S = “jazz”
Output: “kbbb”
方法:可以通过维护存储字符串中每个字符出现次数的频率数组来解决给定的问题。请按照以下步骤解决问题:
- 初始化一个数组freq[26] ,最初所有元素都为0来存储字符串中每个字符的频率。
- 遍历给定的字符串S并将数组freq[]中每个字符S[i]的频率增加1 。
- 使用变量i遍历字符串S并执行以下步骤:
- 将要添加到S[i]的值存储在变量中,添加为(freq[i] % 26) 。
- 如果将add的值添加到S[i] 后, S[i]没有超过字符z ,则将S[i]更新为S[i] + add 。
- 否则,将 add的值更新为(S[i] + add – z) ,然后将S[i]设置为(a + add – 1) 。
- 完成上述步骤后,打印修改后的字符串S 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to modify string by replacing
// characters by the alphabet present at
// distance equal to frequency of the string
void addFrequencyToCharacter(string s)
{
// Stores frequency of characters
int frequency[26] = { 0 };
// Stores length of the string
int n = s.size();
// Traverse the given string S
for (int i = 0; i < n; i++) {
// Increment frequency of
// current character by 1
frequency[s[i] - 'a'] += 1;
}
// Traverse the string
for (int i = 0; i < n; i++) {
// Store the value to be added
// to the current character
int add = frequency[s[i] - 'a'] % 26;
// Check if after adding the
// frequency, the character is
// less than 'z' or not
if (int(s[i]) + add <= int('z'))
s[i] = char(int(s[i]) + add);
// Otherwise, update the value of
// add so that s[i] doesn't exceed 'z'
else {
add = (int(s[i]) + add) - (int('z'));
s[i] = char(int('a') + add - 1);
}
}
// Print the modified string
cout << s;
}
// Driver Code
int main()
{
string str = "geeks";
addFrequencyToCharacter(str);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to modify string by replacing
// characters by the alphabet present at
// distance equal to frequency of the string
static void addFrequencyToCharacter(char[] s)
{
// Stores frequency of characters
int frequency[] = new int[26];
// Stores length of the string
int n = s.length;
// Traverse the given string S
for(int i = 0; i < n; i++)
{
// Increment frequency of
// current character by 1
frequency[s[i] - 'a'] += 1;
}
// Traverse the string
for(int i = 0; i < n; i++)
{
// Store the value to be added
// to the current character
int add = frequency[s[i] - 'a'] % 26;
// Check if after adding the
// frequency, the character is
// less than 'z' or not
if ((int)(s[i]) + add <= (int)('z'))
s[i] = (char)((int)(s[i]) + add);
// Otherwise, update the value of
// add so that s[i] doesn't exceed 'z'
else
{
add = ((int)(s[i]) + add) - ((int)('z'));
s[i] = (char)((int)('a') + add - 1);
}
}
// Print the modified string
System.out.println(s);
}
// Driver Code
public static void main(String[] args)
{
String str = "geeks";
addFrequencyToCharacter(str.toCharArray());
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to modify string by replacing
# characters by the alphabet present at
# distance equal to frequency of the string
def addFrequencyToCharacter(s):
# Stores frequency of characters
frequency = [0] * 26
# Stores length of the string
n = len(s)
# Traverse the given string S
for i in range(n):
# Increment frequency of
# current character by 1
frequency[ord(s[i]) - ord('a')] += 1
# Traverse the string
for i in range(n):
# Store the value to be added
# to the current character
add = frequency[ord(s[i]) - ord('a')] % 26
# Check if after adding the
# frequency, the character is
# less than 'z' or not
if (ord(s[i]) + add <= ord('z')):
s[i] = chr(ord(s[i]) + add)
# Otherwise, update the value of
# add so that s[i] doesn't exceed 'z'
else:
add = (ord(s[i]) + add) - (ord('z'))
s[i] = chr(ord('a') + add - 1)
# Print the modified string
print("".join(s))
# Driver Code
if __name__ == '__main__':
str = "geeks"
addFrequencyToCharacter([i for i in str])
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to modify string by replacing
// characters by the alphabet present at
// distance equal to frequency of the string
static void addFrequencyToCharacter(char[] s)
{
// Stores frequency of characters
int[] frequency = new int[26];
// Stores length of the string
int n = s.Length;
// Traverse the given string S
for(int i = 0; i < n; i++)
{
// Increment frequency of
// current character by 1
frequency[s[i] - 'a'] += 1;
}
// Traverse the string
for(int i = 0; i < n; i++)
{
// Store the value to be added
// to the current character
int add = frequency[s[i] - 'a'] % 26;
// Check if after adding the
// frequency, the character is
// less than 'z' or not
if ((int)(s[i]) + add <= (int)('z'))
s[i] = (char)((int)(s[i]) + add);
// Otherwise, update the value of
// add so that s[i] doesn't exceed 'z'
else
{
add = ((int)(s[i]) + add) - ((int)('z'));
s[i] = (char)((int)('a') + add - 1);
}
}
// Print the modified string
Console.WriteLine(s);
}
// Driver Code
public static void Main(string[] args)
{
string str = "geeks";
addFrequencyToCharacter(str.ToCharArray());
}
}
// This code is contributed by ukasp
Javascript
输出:
hgglt
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live