📌  相关文章
📜  检查是否可以通过给定的操作使字符串S1等于S2

📅  最后修改于: 2021-04-27 05:51:15             🧑  作者: Mango

给定两个字符串S1S2 ,任务是检查是否可以通过对字符串S1执行给定操作来使两个字符串相等。在一个单一的操作中,在奇数索引的任何字符可以与任何其他字符在奇数索引交换,这同样适用于偶数索引的字符。

例子:

方法:

  • S1的偶数索引处的字符创建一个字符串even_s1
  • 类似地,生成字符串even_s2odd_s1odd_s2
  • 对前面步骤中的所有四个字符串排序。
  • 如果even_s1 = even_s2,并且odd_s1 =奇数_s2,则输出
  • 其他打印否,因为字符串不能相等。

下面是上述方法的实现:

C++
// CPP implementation of the approach
#include
using namespace std;
  
// Function to return the string formed 
// by the odd indexed characters of s
string partOdd(string s)
{
    string st = "";
    for(int i = 0; i < s.length(); i++)
    {
        if (i % 2 != 0)
        st += s[i];
    }
    return st;
}
  
// Function to return the string formed 
// by the even indexed characters of s
string partEven(string str)
{
    string s = "";
    for(int i = 0; i < str.length(); i++)
    {
        if (i % 2 == 0)
        s += str[i];
    }
    return s;
}
  
// Function that returns true if s1 
// can be made equal to s2 
// with the given operation
bool canBeMadeEqual(string s1, string s2)
{
      
    // Get the string formed by the 
    // even indexed characters of s1
    string even_s1 = partEven(s1);
      
      
    // Get the string formed by the 
    // even indexed characters of s2
    string even_s2 = partEven(s2);
      
    // Get the string formed by the 
    // odd indexed characters of s1
    string odd_s1 = partOdd(s1);
      
    // Get the string formed by the 
    // odd indexed characters of s2
    string odd_s2 = partOdd(s2);
  
    // Sorting all the lists
    sort(even_s1.begin(), even_s1.end());
    sort(even_s2.begin(), even_s2.end());
    sort(odd_s1.begin(), odd_s1.end());
    sort(odd_s2.begin(), odd_s2.end());
  
    // If the strings can be made equal
    if (even_s1 == even_s2 and odd_s1 == odd_s2)
        return true;
      
    return false;
}
  
// Driver code 
int main()
{
    string s1 = "cdab";
    string s2 = "abcd";
    if(canBeMadeEqual(s1, s2))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
  
// This code is contributed by Surendra_Gangwar


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
    // Function to return the string formed
    // by the odd indexed characters of s
    static String partOdd(String s) 
    {
        String st = "";
        for (int i = 0; i < s.length(); i++)
        {
            if (i % 2 != 0)
                st += s.charAt(i);
        }
  
        return st;
    }
  
    // Function to return the string formed
    // by the even indexed characters of s
    static String partEven(String str)
    {
        String s = "";
        for (int i = 0; i < str.length(); i++) 
        {
            if (i % 2 == 0)
                s += str.charAt(i);
        }
        return s;
    }
  
    // Function that returns true if s1
    // can be made equal to s2
    // with the given operation
    static boolean canBeMadeEqual(String s1,
                                  String s2)
    {
  
        // Get the string formed by the
        // even indexed characters of s1
        char[] even1 = partEven(s1).toCharArray();
  
        // Get the string formed by the
        // even indexed characters of s2
        char[] even2 = partEven(s2).toCharArray();
  
        // Get the string formed by the
        // odd indexed characters of s1
        char[] odd1 = partOdd(s1).toCharArray();
  
        // Get the string formed by the
        // odd indexed characters of s2
        char[] odd2 = partOdd(s2).toCharArray();
  
        // Sorting all the lists
        Arrays.sort(even1);
        Arrays.sort(even2);
        Arrays.sort(odd1);
        Arrays.sort(odd2);
  
        String even_s1 = new String(even1);
        String even_s2 = new String(even2);
        String odd_s1 = new String(odd1);
        String odd_s2 = new String(odd2);
  
        // If the strings can be made equal
        if (even_s1.equals(even_s2) && 
             odd_s1.equals(odd_s2))
            return true;
        return false;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        String s1 = "cdab";
        String s2 = "abcd";
  
        if (canBeMadeEqual(s1, s2))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed by
// sanjeev2552


Python3
# Python3 implementation of the approach
  
# Function to return the string formed 
# by the odd indexed characters of s
def partOdd(s):
    odd = []
    for i in range(len(s)):
        if i % 2 != 0:
            odd.append(s[i])
    return odd
  
# Function to return the string formed 
# by the even indexed characters of s
def partEven(s):
    even = []
    for i in range(len(s)):
        if i % 2 == 0:
            even.append(s[i])
    return even
  
  
# Function that returns true if s1 
# can be made equal to s2 
# with the given operation
def canBeMadeEqual(s1, s2):
      
    # Get the string formed by the 
    # even indexed characters of s1
    even_s1 = partEven(s1)
      
      
    # Get the string formed by the 
    # even indexed characters of s2
    even_s2 = partEven(s2)
      
    # Get the string formed by the 
    # odd indexed characters of s1
    odd_s1 = partOdd(s1)
      
    # Get the string formed by the 
    # odd indexed characters of s2
    odd_s2 = partOdd(s2)
  
    # Sorting all the lists
    even_s1.sort()
    even_s2.sort()
    odd_s1.sort()
    odd_s2.sort()
  
    # If the strings can be made equal
    if even_s1 == even_s2 and odd_s1 == odd_s2:
        return True
      
    return False
  
  
# Driver code 
s1 = "cdab"
s2 = "abcd"
if canBeMadeEqual(s1, s2):
    print("Yes")
else:
    print("No")


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function to return the string formed
    // by the odd indexed characters of s
    static string partOdd(string s) 
    {
        string st = "";
        for (int i = 0; i < s.Length; i++)
        {
            if (i % 2 != 0)
                st += s[i];
        }
  
        return st;
    }
  
    // Function to return the string formed
    // by the even indexed characters of s
    static string partEven(string str)
    {
        string s = "";
        for (int i = 0; i < str.Length; i++) 
        {
            if (i % 2 == 0)
                s += str[i];
        }
        return s;
    }
  
    // Function that returns true if s1
    // can be made equal to s2
    // with the given operation
    static bool canBeMadeEqual(string s1,
                                string s2)
    {
  
        // Get the string formed by the
        // even indexed characters of s1
        char[] even1 = partEven(s1).ToCharArray();
  
        // Get the string formed by the
        // even indexed characters of s2
        char[] even2 = partEven(s2).ToCharArray();
  
        // Get the string formed by the
        // odd indexed characters of s1
        char[] odd1 = partOdd(s1).ToCharArray();
  
        // Get the string formed by the
        // odd indexed characters of s2
        char[] odd2 = partOdd(s2).ToCharArray();
  
        // Sorting all the lists
        Array.Sort(even1);
        Array.Sort(even2);
        Array.Sort(odd1);
        Array.Sort(odd2);
  
        string even_s1 = new string(even1);
        string even_s2 = new string(even2);
        string odd_s1 = new string(odd1);
        string odd_s2 = new string(odd2);
  
        // If the strings can be made equal
        if (even_s1.Equals(even_s2) && 
            odd_s1.Equals(odd_s2))
            return true;
        return false;
    }
  
    // Driver Code
    public static void Main()
    {
        string s1 = "cdab";
        string s2 = "abcd";
  
        if (canBeMadeEqual(s1, s2))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
  
// This code is contributed by AbhiThakur


输出:
Yes