最小化字符重新定位的计数以使所有给定的字符串相等
给定一个大小为N的字符串数组S ,任务是检查是否有可能在任意数量的操作中使所有字符串相等。在一个操作中,可以从字符串中删除任何字符并插入到相同或不同字符串中的任意位置。如果字符串可以相等,则返回所需的最小操作数以及“是”,否则返回“否”。
例子:
Input: N = 3, S = {aaa, bbb, ccc}
Output: Yes 6
Explanation: All three strings can be made equal to string abc, in minimum 6 operations
- {aaa, bbb, ccc} -> {aa, abbb, ccc}
- {aa, abbb, ccc} -> {a, abbb, accc}
- {a, abbb, accc} -> {ab, abb, accc}
- {ab, abb, accc} -> {ab, ab, abccc}
- {ab, ab, abccc} -> {abc, ab, abcc}
- {abc, ab, abccc} -> {abc, abc, abc}
Input: N = 3, S = {aba, bbb, cda}
Output: No
方法:如果字母应该在所有字符串中均匀分布,则可以实现使所有字符串相等的想法。即每个字符的频率应该能被N整除。请按照以下步骤解决给定的问题:
- 初始化一个向量,比如freq[]来存储字符串中每个字符的频率。
- 遍历字符串数组。
- 将每个字符的频率存储在向量hashmap中。
- 最后,检查任何字符的频率是否不是N的倍数,然后打印No
- 否则,将每个字符的频率除以N ,以便在 N 个字符串中均匀分布
- 结果答案将是原始字符串中额外字符相对于结果相等字符串的计数
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if strings
// can be formed equal or not
void solve(string S[], int N)
{
// Vector to store the frequency
// of characters
vector freq(26, 0);
// Traversing the array of strings
for (int i = 0; i < N; i++) {
// Traversing characters of the
// string
for (auto x : S[i]) {
// Updating the frequency
freq[x - 'a']++;
}
}
// Checking for each character of
// alphabet
for (int i = 0; i < 26; i++) {
// If frequency is not multiple
// of N
if (freq[i] % N != 0) {
cout << "No\n";
return;
}
}
// Divide frequency of each character
// with N
for (int i = 0; i < 26; i++)
freq[i] /= N;
// Store the count of minimum
// operations
int ans = 0;
for (int i = 0; i < N; i++) {
// Store frequencies of characters
// in the original string
vector vis(26, 0);
for (char c : S[i])
vis++;
// Get the count of extra characters
for (int i = 0; i < 26; i++) {
if (freq[i] > 0 && vis[i] > 0) {
ans += abs(freq[i] - vis[i]);
}
}
}
cout << "Yes " << ans << endl;
return;
}
// Driver function
int main()
{
int N = 3;
string S[N] = { "aaa", "bbb", "ccc" };
solve(S, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if Strings
// can be formed equal or not
static void solve(String S[], int N)
{
// Vector to store the frequency
// of characters
int []freq = new int[26];
// Traversing the array of Strings
for (int i = 0; i < N; i++) {
// Traversing characters of the
// String
for (int x : S[i].toCharArray()) {
// Updating the frequency
freq[x - 'a']++;
}
}
// Checking for each character of
// alphabet
for (int i = 0; i < 26; i++) {
// If frequency is not multiple
// of N
if (freq[i] % N != 0) {
System.out.print("No\n");
return;
}
}
// Divide frequency of each character
// with N
for (int i = 0; i < 26; i++)
freq[i] /= N;
// Store the count of minimum
// operations
int ans = 0;
for (int s = 0; s < N; s++) {
// Store frequencies of characters
// in the original String
int []vis = new int[26];
for (char c : S[s].toCharArray())
vis++;
// Get the count of extra characters
for (int i = 0; i < 26; i++) {
if (freq[i] > 0 && vis[i] > 0) {
ans += Math.abs(freq[i] - vis[i]);
}
}
}
System.out.print("Yes " + ans +"\n");
return;
}
// Driver function
public static void main(String[] args)
{
int N = 3;
String S[] = { "aaa", "bbb", "ccc" };
solve(S, N);
}
}
// This code is contributed by shikhasingrajput
Python3
# python program for the above approach
# Function to check if strings
# can be formed equal or not
def solve(S, N):
# Vector to store the frequency
# of characters
freq = [0 for _ in range(26)]
# Traversing the array of strings
for i in range(0, N):
# Traversing characters of the
# string
for x in S[i]:
# Updating the frequency
freq[ord(x) - ord('a')] += 1
# Checking for each character of
# alphabet
for i in range(0, 26):
# If frequency is not multiple
# of N
if (freq[i] % N != 0):
print("No")
return
# Divide frequency of each character
# with N
for i in range(0, 26):
freq[i] //= N
# Store the count of minimum
# operations
ans = 0
for i in range(0, N):
# Store frequencies of characters
# in the original string
vis = [0 for _ in range(26)]
for c in S[i]:
vis[ord(c) - ord('a')] += 1
# Get the count of extra characters
for i in range(0, 26):
if (freq[i] > 0 and vis[i] > 0):
ans += abs(freq[i] - vis[i])
print(f"Yes {ans}")
return
# Driver function
if __name__ == "__main__":
N = 3
S = ["aaa", "bbb", "ccc"]
solve(S, N)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if Strings
// can be formed equal or not
static void solve(String []S, int N)
{
// List to store the frequency
// of characters
int []freq = new int[26];
// Traversing the array of Strings
for(int i = 0; i < N; i++)
{
// Traversing characters of the
// String
foreach (int x in S[i].ToCharArray())
{
// Updating the frequency
freq[x - 'a']++;
}
}
// Checking for each character of
// alphabet
for(int i = 0; i < 26; i++)
{
// If frequency is not multiple
// of N
if (freq[i] % N != 0)
{
Console.Write("No\n");
return;
}
}
// Divide frequency of each character
// with N
for(int i = 0; i < 26; i++)
freq[i] /= N;
// Store the count of minimum
// operations
int ans = 0;
for(int s = 0; s < N; s++)
{
// Store frequencies of characters
// in the original String
int []vis = new int[26];
foreach (char c in S[s].ToCharArray())
vis++;
// Get the count of extra characters
for(int i = 0; i < 26; i++)
{
if (freq[i] > 0 && vis[i] > 0)
{
ans += Math.Abs(freq[i] - vis[i]);
}
}
}
Console.Write("Yes " + ans + "\n");
return;
}
// Driver code
public static void Main(String[] args)
{
int N = 3;
String []S = { "aaa", "bbb", "ccc" };
solve(S, N);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
Yes 6
时间复杂度: O(N*26)
辅助空间: O(1)