📌  相关文章
📜  检查是否可以通过交换相同或不同字符串的两个字符来使给定的字符串相同

📅  最后修改于: 2021-10-27 07:58:23             🧑  作者: Mango

给定一个大小为N的等长字符串数组arr[] ,任务是检查是否可以通过重复交换给定数组中相同或不同字符串的任何字符对来使所有字符串相等。如果发现是真的,则打印“YES” 。否则,打印“NO”

例子:

方法:我们的想法是计算所有字符串的每个不同字符的频率,并检查是否每个不同字符的频率是整除N或不是。如果发现是真的,则打印“YES” 。否则,打印“NO” 。请按照以下步骤解决问题:

  • 初始化一个数组,比如cntFreq[] ,以存储所有字符串的每个不同字符的频率。
  • 遍历阵列和用于遇到的每一个字符串,计数字符串的每个不同的字符的频率。
  • 使用变量i遍历数组cntFreq[] 。对于每个i字符,检查cntFreq[i]是否可以被N整除。如果发现是假的,则打印“NO”
  • 否则,打印“YES”

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check if all strings can be
// made equal by swapping any pair of
// characters from the same or different strings
bool isEqualStrings(string arr[], int N)
{
    // Stores length of string
    int M = arr[0].length();
 
    // Store frequency of each distinct
    // character of the strings
    int cntFreq[256] = { 0 };
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Iterate over the characters
        for (int j = 0; j < M; j++) {
 
            // Update frequency
            // of arr[i][j]
            cntFreq[arr[i][j] - 'a']++;
        }
    }
 
    // Traverse the array cntFreq[]
    for (int i = 0; i < 256; i++) {
 
        // If cntFreq[i] is
        // divisible by N or not
        if (cntFreq[i] % N != 0) {
 
            return false;
        }
    }
 
    return true;
}
 
// Driver Code
int main()
{
    string arr[] = { "aab", "bbc", "cca" };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    if (isEqualStrings(arr, N)) {
 
        cout << "YES";
    }
    else {
 
        cout << "NO";
    }
 
    return 0;
}


Java
// Java program to implement
// the above approach 
class GFG{
    
// Function to check if all strings can be
// made equal by swapping any pair of
// characters from the same or different strings
static boolean isEqualStrings(String[] arr, int N)
{
     
    // Stores length of string
    int M = arr[0].length();
  
    // Store frequency of each distinct
    // character of the strings
    int[] cntFreq = new int[256];
    for(int i = 0; i < N; i++)
    {
        cntFreq[i] = 0;
    }
     
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Iterate over the characters
        for(int j = 0; j < M; j++)
        {
             
            // Update frequency
            // of arr[i].charAt(j)
            cntFreq[arr[i].charAt(j) - 'a'] += 1;
        }
    }
  
    // Traverse the array cntFreq[]
    for(int i = 0; i < 256; i++)
    {
         
        // If cntFreq[i] is
        // divisible by N or not
        if (cntFreq[i] % N != 0)
        {
            return false;
        }
    }
    return true;
}
    
// Driver Code
public static void main(String[] args)
{
    String[] arr = { "aab", "bbc", "cca" };
    int N = arr.length;
     
    // Function Call
    if (isEqualStrings(arr, N))
    {
        System.out.println("YES");
    }
    else
    {
        System.out.println("NO");
    }
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program to implement
# the above approach
 
# Function to check if all strings can
# be made equal by swapping any pair of
# characters from the same or different
# strings
def isEqualStrings(arr, N):
 
    # Stores length of string
    M = len(arr[0])
 
    # Store frequency of each distinct
    # character of the strings
    cntFreq = [0] * 256
 
    # Traverse the array
    for i in range(N):
 
        # Iterate over the characters
        for j in range(M): 
             
            # Update frequency
            # of arr[i][j]
            cntFreq[ord(arr[i][j]) - ord('a')] += 1
             
    # Traverse the array cntFreq[]
    for i in range(256):
         
        # If cntFreq[i] is
        # divisible by N or not
        if (cntFreq[i] % N != 0):
            return False
         
    return True
 
# Driver Code
if __name__ == "__main__" :
     
    arr = [ "aab", "bbc", "cca" ]
    N = len(arr)
 
    # Function Call
    if isEqualStrings(arr, N):
        print("YES")
    else:
        print("NO")
     
# This code is contributed by jana_sayantan


C#
// C# program to implement
// the above approach 
using System;
    
class GFG{
    
// Function to check if all strings can be
// made equal by swapping any pair of
// characters from the same or different strings
static bool isEqualStrings(string[] arr, int N)
{
     
    // Stores length of string
    int M = arr[0].Length;
  
    // Store frequency of each distinct
    // character of the strings
    int[] cntFreq = new int[256];
    for(int i = 0; i < N; i++)
    {
        cntFreq[i] = 0;
    }
     
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Iterate over the characters
        for(int j = 0; j < M; j++)
        {
             
            // Update frequency
            // of arr[i][j]
            cntFreq[arr[i][j] - 'a'] += 1;
        }
    }
  
    // Traverse the array cntFreq[]
    for(int i = 0; i < 256; i++)
    {
         
        // If cntFreq[i] is
        // divisible by N or not
        if (cntFreq[i] % N != 0)
        {
            return false;
        }
    }
    return true;
}
    
// Driver Code
public static void Main()
{
    string[] arr = { "aab", "bbc", "cca" };
    int N = arr.Length;
  
    // Function Call
    if (isEqualStrings(arr, N))
    {
        Console.WriteLine("YES");
    }
    else
    {
        Console.WriteLine("NO");
    }
}
}
 
// This code is contributed by susmitakundugoaldanga


Javascript


输出:
YES

时间复杂度:O(N * M + 256),其中M是字符串的长度
辅助空间: O(256)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程