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
function check ( s1 , s2 ) { let s1_0 = 0 , s2_0 = 0 ; for ( let i = 0 ; i < s1 . length ; i ++ ) { if ( s1 [ i ] == ' ' ) { s1_0 ++ ; } if ( s2 [ i ] == ' ' ) { s2_0 ++ ; } } if ( s1_0 != s2_0 ) { document . write ( " " ) ; return ; } else { let Count1 = 0 , Count2 = 0 ; for ( let i = 0 ; i < s1 . length ; i ++ ) { if ( s1 [ i ] == ' ' ) { Count1 ++ ; } if ( s2 [ i ] == ' ' ) { Count2 ++ ; } if ( Count1 < Count2 ) { document . write ( " " + " " ) ; return ; } } document . write ( " " + " " ) ; } } let s1 = " " ; let s2 = " " ; check ( s1 , s2 ) ; s1 = " " ; s2 = " " ; check ( s1 , s2 ) ;