Check if two binary strings can be made equal by swapping 1 s occurring before 0 s | Function to check if it is possible to make two binary strings equal by given operations ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str1 ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str2 ; Stores current count of 1 's  presenty in the string str1 ; Count the number of 1 ' s ▁ and ▁ 0' s present in the strings str1 and str2 ; If the number of 1 ' s ▁ and ▁ 0' s are not same of the strings str1 and str2 then print not possible ; Traversing through the strings str1 and str2 ; If the str1 character not equals to str2 character ; Swaps 0 with 1 of the string str1 ; Breaks the loop as the count of 1 's is zero. Hence, no swaps  possible ; Swaps 1 with 0 in the string str1 ; Print not possible ; Given Strings ; Function Call
function isBinaryStringsEqual ( list1 , list2 ) { var str1 = list1 . split ( " " ) ; var str2 = list2 . split ( " " ) ; var str1Zeros = 0 , str1Ones = 0 ; var str2Zeros = 0 , str2Ones = 0 ; var flag = 0 ; var curStr1Ones = 0 ; for ( var i = 0 ; i < str1 . length ; i ++ ) { if ( str1 [ i ] === " " ) { str1Ones ++ ; } else if ( str1 [ i ] === " " ) { str1Zeros ++ ; } if ( str2 [ i ] === " " ) { str2Ones ++ ; } else if ( str2 [ i ] === " " ) { str2Zeros ++ ; } } if ( str1Zeros !== str2Zeros && str1Ones !== str2Ones ) { document . write ( " " ) ; } else { for ( var i = 0 ; i < str1 . length ; i ++ ) { if ( str1 [ i ] !== str2 [ i ] ) { if ( str1 [ i ] === " " && curStr1Ones > 0 ) { str1 [ i ] = " " ; curStr1Ones -- ; } if ( str1 [ i ] === " " && curStr1Ones === 0 ) { flag ++ ; break ; } if ( str1 [ i ] === " " && str2 [ i ] === " " ) { str1 [ i ] = " " ; curStr1Ones ++ ; } } } if ( flag === 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } var str1 = " " ; var str2 = " " ; isBinaryStringsEqual ( str1 , str2 ) ;