Check if two binary strings can be made equal by swapping pairs of unequal characters | Function to check if a string s1 can be converted into s2 ; Count of '0' in strings in s1 and s2 ; Iterate both the strings and count the number of occurrences of ; Count is not equal ; Iterating over both the arrays and count the number of occurrences of '0 ; If the count of occurrences of '0' in S2 exceeds that in S1 ; Driver code
def check ( s1 , s2 ) : NEW_LINE INDENT s1_0 = 0 NEW_LINE s2_0 = 0 NEW_LINE for i in range ( len ( s1 ) ) : NEW_LINE INDENT if ( s1 [ i ] == '0' ) : NEW_LINE INDENT s1_0 += 1 NEW_LINE DEDENT if ( s2 [ i ] == '0' ) : NEW_LINE INDENT s2_0 += 1 NEW_LINE DEDENT DEDENT if ( s1_0 != s2_0 ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT Count1 = 0 NEW_LINE Count2 = 0 ; NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT for i in range ( len ( s1 ) ) : NEW_LINE INDENT if ( s1 [ i ] == '0' ) : NEW_LINE INDENT Count1 += 1 NEW_LINE DEDENT if ( s2 [ i ] == '0' ) : NEW_LINE INDENT Count2 += 1 NEW_LINE DEDENT if ( Count1 < Count2 ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " YES " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = "100111" NEW_LINE s2 = "111010" NEW_LINE check ( s1 , s2 ) NEW_LINE s1 = "110100" NEW_LINE s2 = "010101" NEW_LINE check ( s1 , s2 ) NEW_LINE DEDENT