给定一个由字母[‘A’ – ‘Z’]组成的字符串S ,任务是找到使每个字符的频率相等所需的最少操作次数。在一次操作中,可以选择字符串的任何字符并用另一个有效字符替换。
例子:
Input: S = “ABCB”
Output: 1
Explanation:
In the given string character ‘C’ can be replaced by ‘A’, such that occurrence of every character becomes equal to 2.
Updated String = “ABAB”
Input: S = “BBC”
Output : 1
Explanation:
In the given string character ‘C’ can be replaced by ‘B’, such that occurrence of every character becomes equal to 3.
Updated string = “BBB”
方法:思路是找出字符串中每个字符出现的频率,然后根据出现的频率降序对字符进行排序。最后,我们可以检查字符串中产生最少要更改的字符数的每个字符,并打印要更改的最少字符数。
下面是上述方法的实现:
C++
// C++ implementation to find the
// Minimum characters to be replaced
// to make frequency of all characters same
#include
using namespace std;
// Function to find the
// Minimum operations to convert
// given string to another with
// equal frequencies of characters
int minOperations(string s)
{
// Frequency of characters
int freq[26] = { 0 };
int n = s.length();
// Loop to find the Frequency
// of each character
for (int i = 0; i < n; i++) {
freq[s[i] - 'A']++;
}
// Sort in decreasing order
// based on frequency
sort(freq, freq + 26, greater());
// Maximum possible answer
int answer = n;
// Loop to find the minimum operations
// required such that frequency of
// every character is equal
for (int i = 1; i <= 26; i++) {
if (n % i == 0) {
int x = n / i;
int y = 0;
for (int j = 0; j < i; j++) {
y += min(freq[j], x);
}
answer = min(answer, n - y);
}
}
return answer;
}
// Driver Code
int main()
{
string s = "BBC";
cout << minOperations(s);
return 0;
}
Java
// Java implementation to find the
// Minimum characters to be replaced
// to make frequency of all characters same
import java.util.*;
class GFG{
// Function to find the
// Minimum operations to convert
// given String to another with
// equal frequencies of characters
static int minOperations(String s)
{
// Frequency of characters
Integer freq[] = new Integer[26];
Arrays.fill(freq, 0);
int n = s.length();
// Loop to find the Frequency
// of each character
for (int i = 0; i < n; i++) {
freq[s.charAt(i) - 'A']++;
}
// Sort in decreasing order
// based on frequency
Arrays.sort(freq, Collections.reverseOrder());
// Maximum possible answer
int answer = n;
// Loop to find the minimum operations
// required such that frequency of
// every character is equal
for (int i = 1; i <= 26; i++) {
if (n % i == 0) {
int x = n / i;
int y = 0;
for (int j = 0; j < i; j++) {
y += Math.min(freq[j], x);
}
answer = Math.min(answer, n - y);
}
}
return answer;
}
// Driver Code
public static void main(String[] args)
{
String s = "BBC";
System.out.print(minOperations(s));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation to find the
# minimum characters to be replaced
# to make frequency of all characters same
# Function to find the minimum
# operations to convert given
# string to another with equal
# frequencies of characters
def minOperations(s):
# Frequency of characters
freq = [0] * 26
n = len(s)
# Loop to find the Frequency
# of each character
for i in range(n):
freq[ord(s[i]) - ord('A')] += 1
# Sort in decreasing order
# based on frequency
freq.sort(reverse = True)
# Maximum possible answer
answer = n
# Loop to find the minimum operations
# required such that frequency of
# every character is equal
for i in range(1, 27):
if (n % i == 0):
x = n // i
y = 0
for j in range(i):
y += min(freq[j], x)
answer = min(answer, n - y)
return answer
# Driver Code
if __name__ == "__main__":
s = "BBC"
print (minOperations(s))
# This code is contributed by chitranayal
C#
// C# implementation to find the minimum
// characters to be replaced to make
// frequency of all characters same
using System;
class GFG{
// Function to find the minimum
// operations to convert given
// string to another with equal
// frequencies of characters
static int minOperations(String s)
{
// Frequency of characters
int []freq = new int[26];
int n = s.Length;
// Loop to find the frequency
// of each character
for(int i = 0; i < n; i++)
{
freq[s[i] - 'A']++;
}
// Sort in decreasing order
// based on frequency
Array.Sort(freq);
Array.Reverse(freq);
// Maximum possible answer
int answer = n;
// Loop to find the minimum operations
// required such that frequency of
// every character is equal
for(int i = 1; i <= 26; i++)
{
if (n % i == 0)
{
int x = n / i;
int y = 0;
for(int j = 0; j < i; j++)
{
y += Math.Min(freq[j], x);
}
answer = Math.Min(answer, n - y);
}
}
return answer;
}
// Driver Code
public static void Main(String[] args)
{
String s = "BBC";
Console.Write(minOperations(s));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
1
时间复杂度: O(n)
辅助空间: O(26)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。