📌  相关文章
📜  检查通过与相邻的复制自己的字符两个字符串是否可以进行平等

📅  最后修改于: 2021-04-27 06:25:50             🧑  作者: Mango

给定两个字符串str1str2 ,任务是通过复制字符串的任何字符及其相邻字符来检查两个字符串是否相等。注意,该操作可以执行多次。

例子:

的方法:在将被制造为使字符串与所述给定的操作相同,它们必须是相等长度的并且必须有至少一个字符是在两个字符串的常见。要检查,创建一个频率数组频率[]将存储STR1的所有字符的频率,然后对STR2的每个字符,如果其在STR1频率大于0,那么它可以使两个字符串相等。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
#define MAX 26
  
// Function that returns true if both
// the strings can be made equal
// with the given operation
bool canBeMadeEqual(string str1, string str2)
{
    int len1 = str1.length();
    int len2 = str2.length();
  
    // Lengths of both the strings
    // have to be equal
    if (len1 == len2) {
  
        // To store the frequency of the
        // characters of str1
        int freq[MAX];
        for (int i = 0; i < len1; i++) {
            freq[str1[i] - 'a']++;
        }
  
        // For every character of str2
        for (int i = 0; i < len2; i++) {
  
            // If current character of str2
            // also appears in str1
            if (freq[str2[i] - 'a'] > 0)
                return true;
        }
    }
  
    return false;
}
  
// Driver code
int main()
{
    string str1 = "abc", str2 = "defa";
  
    if (canBeMadeEqual(str1, str2))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the above approach 
class GFG 
{
    static int MAX = 26; 
      
    // Function that returns true if both 
    // the strings can be made equal 
    // with the given operation 
    static boolean canBeMadeEqual(String str1,
                                  String str2) 
    { 
        int len1 = str1.length(); 
        int len2 = str2.length(); 
      
        // Lengths of both the strings 
        // have to be equal 
        if (len1 == len2) 
        { 
      
            // To store the frequency of the 
            // characters of str1 
            int freq[] = new int[MAX]; 
              
            for (int i = 0; i < len1; i++) 
            { 
                freq[str1.charAt(i) - 'a']++; 
            } 
      
            // For every character of str2 
            for (int i = 0; i < len2; i++)
            { 
      
                // If current character of str2 
                // also appears in str1 
                if (freq[str2.charAt(i) - 'a'] > 0) 
                    return true; 
            } 
        } 
        return false; 
    } 
      
    // Driver code 
    public static void main (String[] args) 
    { 
        String str1 = "abc", str2 = "defa"; 
      
        if (canBeMadeEqual(str1, str2)) 
            System.out.println("Yes"); 
        else
            System.out.println("No"); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
MAX = 26
  
# Function that returns true if both
# the strings can be made equal
# with the given operation
def canBeMadeEqual(str1, str2):
    len1 = len(str1)
    len2 = len(str2)
  
    # Lengths of both the strings
    # have to be equal
    if (len1 == len2):
  
        # To store the frequency of the
        # characters of str1
        freq = [0 for i in range(MAX)]
        for i in range(len1):
            freq[ord(str1[i]) - ord('a')] += 1
  
        # For every character of str2
        for i in range(len2):
  
            # If current character of str2
            # also appears in str1
            if (freq[ord(str2[i]) - ord('a')] > 0):
                return True
  
    return False
  
# Driver code
str1 = "abc"
str2 = "defa"
  
if (canBeMadeEqual(str1, str2)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the above approach 
using System;
  
class GFG 
{
    static int MAX = 26; 
      
    // Function that returns true if both 
    // the strings can be made equal 
    // with the given operation 
    static Boolean canBeMadeEqual(String str1,
                                   String str2) 
    { 
        int len1 = str1.Length; 
        int len2 = str2.Length; 
      
        // Lengths of both the strings 
        // have to be equal 
        if (len1 == len2) 
        { 
      
            // To store the frequency of the 
            // characters of str1 
            int []freq = new int[MAX]; 
              
            for (int i = 0; i < len1; i++) 
            { 
                freq[str1[i] - 'a']++; 
            } 
      
            // For every character of str2 
            for (int i = 0; i < len2; i++)
            { 
      
                // If current character of str2 
                // also appears in str1 
                if (freq[str2[i] - 'a'] > 0) 
                    return true; 
            } 
        } 
        return false; 
    } 
      
    // Driver code 
    public static void Main (String []args) 
    { 
        String str1 = "abc", str2 = "defa"; 
      
        if (canBeMadeEqual(str1, str2)) 
            Console.WriteLine("Yes"); 
        else
            Console.WriteLine("No"); 
    } 
}
  
// This code is contributed by Arnab Kundu


输出:
No