Check if two binary strings can be made equal by swapping pairs of unequal characters | Java program to implement the above approach ; 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
import java . util . * ; class GFG { static void check ( String s1 , String s2 ) { int s1_0 = 0 , s2_0 = 0 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( s1 . charAt ( i ) == '0' ) { s1_0 ++ ; } if ( s2 . charAt ( i ) == '0' ) { s2_0 ++ ; } } if ( s1_0 != s2_0 ) { System . out . println ( " NO " ) ; return ; } else { int Count1 = 0 , Count2 = 0 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( s1 . charAt ( i ) == '0' ) { Count1 ++ ; } if ( s2 . charAt ( i ) == '0' ) { Count2 ++ ; } if ( Count1 < Count2 ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } } public static void main ( String [ ] args ) { String s1 = "100111" ; String s2 = "111010" ; check ( s1 , s2 ) ; s1 = "110100" ; s2 = "010101" ; check ( s1 , s2 ) ; } }