Given two binary strings S1 and S2 of length N (1 ≤ N ≤ 105), the task is to check if it is possible to convert the string S1 to
S2 by performing the following operations any number of times:
- Select any two indices i and j (1 ≤ i < j ≤ N) such that S1[i] is ‘0’ and S1[j] is ‘1’.
- Swap S1[i] with S1[j].
Examples:
Input: S1 =”100111″, S2 = “111010”
Output: YES
Explanation:Swap S[2] and S[3] with S[4] and S[6] respectively.Input: S1 = “110100”, S2 = “010101”
Output: NO
Approach: Follow the steps below to solve the problem:
- Check if the number of occurrences of character ‘0’ and ‘1’ in both the strings are equal. If not found to be true, then it is impossible to transform string S1 into S2.
- If the number of characters are equal, then we move to the next step. By the given condition it is possible to move the ‘0’ only in forward direction by swapping with letter ‘1’ in string S1.
- Hence, iterate characters of both the strings and count the number of occurrences of ‘0’ in both strings. If at any point the count of characters of ‘0’ in string S2 becomes strictly greater than count of occurrences in string S1, terminate the loop and print “NO”.
- If both the strings iterated successfully, print “YES”.
Below is the implementation of above approach:
C++
// C++ Program to implement// of above approach#include <bits/stdc++.h>using namespace std;// Function to check if a string// s1 can be converted into s2void check(string s1, string s2){ // Count of '0' in strings in s1 and s2 int s1_0 = 0, s2_0 = 0; // Iterate both the strings and // count the number of occurrences of for (int i = 0; i < s1.size(); i++) { if (s1[i] == '0') { s1_0++; } if (s2[i] == '0') { s2_0++; } } // Count is not equal if (s1_0 != s2_0) { cout << "NO" << endl; return; } else { int Count1 = 0, Count2 = 0; // Iterating over both the // arrays and count the // number of occurrences of '0' for (int i = 0; i < s1.size(); i++) { if (s1[i] == '0') { Count1++; } if (s2[i] == '0') { Count2++; } // If the count of occurrences // of '0' in S2 exceeds that in S1 if (Count1 < Count2) { cout << "NO" << endl; return; } } cout << "YES" << endl; }}// Driver programint main(){ string s1 = "100111"; string s2 = "111010"; check(s1, s2); s1 = "110100"; s2 = "010101"; check(s1, s2); return 0;} |
Java
// Java program to implement// the above approachimport java.util.*;class GFG{ // Function to check if a string // s1 can be converted into s2 static void check(String s1, String s2) { // Count of '0' in strings in s1 and s2 int s1_0 = 0, s2_0 = 0; // Iterate both the strings and // count the number of occurrences of for(int i = 0; i < s1.length(); i++) { if (s1.charAt(i) == '0') { s1_0++; } if (s2.charAt(i) == '0') { s2_0++; } } // Count is not equal if (s1_0 != s2_0) { System.out.println("NO"); return; } else { int Count1 = 0, Count2 = 0; // Iterating over both the // arrays and count the // number of occurrences of '0' for(int i = 0; i < s1.length(); i++) { if (s1.charAt(i) == '0') { Count1++; } if (s2.charAt(i) == '0') { Count2++; } // If the count of occurrences // of '0' in S2 exceeds that in S1 if (Count1 < Count2) { System.out.println("NO"); return; } } System.out.println("YES"); } } // Driver Codepublic static void main(String[] args){ String s1 = "100111"; String s2 = "111010"; check(s1, s2); s1 = "110100"; s2 = "010101"; check(s1, s2);}}// This code is contributed by code_hunt. |
Python3
# Python3 program to implement# of above approach# Function to check if a string# s1 can be converted into s2def check(s1, s2): # Count of '0' in strings in s1 and s2 s1_0 = 0 s2_0 = 0 # Iterate both the strings and # count the number of occurrences of for i in range(len(s1)): if (s1[i] == '0'): s1_0 += 1 if (s2[i] == '0'): s2_0 += 1 # Count is not equal if (s1_0 != s2_0): print("NO") return else: Count1 = 0 Count2 = 0; # Iterating over both the # arrays and count the # number of occurrences of '0' for i in range(len(s1)): if (s1[i] == '0'): Count1 += 1 if (s2[i] == '0'): Count2 += 1 # If the count of occurrences # of '0' in S2 exceeds that in S1 if (Count1 < Count2): print("NO") return print("YES")# Driver codeif __name__ == "__main__": s1 = "100111" s2 = "111010" check(s1, s2) s1 = "110100" s2 = "010101" check(s1, s2)# This code is contributed by chitranayal |
C#
// C# program to implement // of above approach using System;class GFG{ // Function to check if a string // s1 can be converted into s2 static void check(string s1, string s2) { // Count of '0' in strings in s1 and s2 int s1_0 = 0, s2_0 = 0; // Iterate both the strings and // count the number of occurrences of for(int i = 0; i < s1.Length; i++) { if (s1[i] == '0') { s1_0++; } if (s2[i] == '0') { s2_0++; } } // Count is not equal if (s1_0 != s2_0) { Console.WriteLine("NO"); return; } else { int Count1 = 0, Count2 = 0; // Iterating over both the // arrays and count the // number of occurrences of '0' for(int i = 0; i < s1.Length; i++) { if (s1[i] == '0') { Count1++; } if (s2[i] == '0') { Count2++; } // If the count of occurrences // of '0' in S2 exceeds that in S1 if (Count1 < Count2) { Console.WriteLine("NO"); return; } } Console.WriteLine("YES"); } } // Driver codestatic void Main() { string s1 = "100111"; string s2 = "111010"; check(s1, s2); s1 = "110100"; s2 = "010101"; check(s1, s2);}}// This code is contributed by divyesh072019 |
YES NO
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

