Check whether the string S1 can be made equal to S2 with the given operation

28 Jun, 2022
Given two string S1 and S2, the task is to check whether both the strings can be made equal by performing the given operation on string S1. In a single operation, any character at an odd index can be swapped with any other character at an odd index, the same goes for the characters at even indices.Examples:  
Input: S1 = “abcd”, S2 = “cbad” Output: Yes Swap ‘a’ and ‘c’ in S1 and the resultant string is equal to S2.Input: S1 = “abcd”, S2 = “abcdcd” Output: No 
Approach:  
Create a string even_s1 from the characters at even indices from S1.
Similarly, generate the strings even_s2, odd_s1 and odd_s2.
Sort all the four strings from the previous steps.
If even_s1 = even_s2 and odd_s1 = odd_s2 then print Yes.
Else print No as the strings cannot be made equal.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// CPP implementation of the approach#include<bits/stdc++.h>using namespace std; // Function to return the string formed// by the odd indexed characters of sstring 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 sstring 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 operationbool 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 codeint 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 implementation of the approachimport 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 implementation of the approach # Function to return the string formed# by the odd indexed characters of sdef 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 sdef 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 operationdef 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 codes1 = "cdab"s2 = "abcd"if canBeMadeEqual(s1, s2):    print("Yes")else:    print("No")
// C# implementation of the approachusing 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
<script>       // JavaScript implementation of the approach             // Function to return the string formed      // by the odd indexed characters of s      function partOdd(s) {        var st = "";        for (var 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      function partEven(str) {        var s = "";        for (var 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      function canBeMadeEqual(s1, s2) {        // Get the string formed by the        // even indexed characters of s1        var even1 = partEven(s1).split("");         // Get the string formed by the        // even indexed characters of s2        var even2 = partEven(s2).split("");         // Get the string formed by the        // odd indexed characters of s1        var odd1 = partOdd(s1).split("");         // Get the string formed by the        // odd indexed characters of s2        var odd2 = partOdd(s2).split("");         // Sorting all the lists        even1.sort();        even2.sort();        odd1.sort();        odd2.sort();         var even_s1 = even1.join("");        var even_s2 = even2.join("");        var odd_s1 = odd1.join("");        var odd_s2 = odd2.join("");         // If the strings can be made equal        if (even_s1 === even_s2 && odd_s1 === odd_s2) return true;        return false;      }       // Driver Code      var s1 = "cdab";      var s2 = "abcd";       if (canBeMadeEqual(s1, s2)) document.write("Yes");      else document.write("No");        </script>
Yes
 
Time Complexity: O(n*log(n)+m*log(m)) where n and m are the lengths of the string.Auxiliary Space: O(n+m)
SURENDRA_GANGWAR
sanjeev2552
abhaysingh290895
rdtank
pushpeshrajdx01
Constructive Algorithms
Sorting
Strings
Strings
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.