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 prnot 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 ; Prnot possible ; Given Strings ; Function Call
def isBinaryStringsEqual ( list1 , list2 ) : NEW_LINE INDENT str1 = list ( list1 ) NEW_LINE str2 = list ( list2 ) NEW_LINE str1Zeros = 0 NEW_LINE str1Ones = 0 NEW_LINE str2Zeros = 0 NEW_LINE str2Ones = 0 NEW_LINE flag = 0 NEW_LINE curStr1Ones = 0 NEW_LINE for i in range ( len ( str1 ) ) : NEW_LINE INDENT if ( str1 [ i ] == '1' ) : NEW_LINE INDENT str1Ones += 1 NEW_LINE DEDENT elif ( str1 [ i ] == '0' ) : NEW_LINE INDENT str1Zeros += 1 NEW_LINE DEDENT if ( str2 [ i ] == '1' ) : NEW_LINE INDENT str2Ones += 1 NEW_LINE DEDENT elif ( str2 [ i ] == '0' ) : NEW_LINE INDENT str2Zeros += 1 NEW_LINE DEDENT DEDENT if ( str1Zeros != str2Zeros and str1Ones != str2Ones ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( len ( str1 ) ) : NEW_LINE INDENT if ( str1 [ i ] != str2 [ i ] ) : NEW_LINE INDENT if ( str1 [ i ] == '0' and curStr1Ones > 0 ) : NEW_LINE INDENT str1 [ i ] = '1' NEW_LINE curStr1Ones -= 1 NEW_LINE DEDENT if ( str1 [ i ] == '0' and curStr1Ones == 0 ) : NEW_LINE INDENT flag += 1 NEW_LINE break NEW_LINE DEDENT if ( str1 [ i ] == '1' and str2 [ i ] == '0' ) : NEW_LINE INDENT str1 [ i ] = '0' NEW_LINE curStr1Ones += 1 NEW_LINE DEDENT DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " Possible " ) NEW_LINE DEDENT else : NEW_LINE print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT str1 = "0110" NEW_LINE str2 = "0011" NEW_LINE isBinaryStringsEqual ( str1 , str2 ) NEW_LINE