Check if a Binary String can be converted to another by reversing substrings consisting of even number of 1 s | Function to check if string A can be transformed to string B by reversing substrings of A having even number of 1 s ; Store the size of string A ; Store the size of string B ; Store the count of 1 s in A and B ; Stores cntA for string A and cntB for string B ; Traverse the string A ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1A or even1A depending whether count1A is odd or even ; Traverse the string B ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1B or even1B depending whether count1B is odd or even ; If the condition is satisfied ; If True , prYes ; Otherwise , prNo ; Driver Code ; Function Call
def canTransformStrings ( A , B ) : NEW_LINE INDENT n1 = len ( A ) ; NEW_LINE n2 = len ( B ) ; NEW_LINE count1A = 0 ; NEW_LINE count1B = 0 ; NEW_LINE odd1A = 0 ; odd1B = 0 ; NEW_LINE even1A = 0 ; even1B = 0 ; NEW_LINE for i in range ( n1 ) : NEW_LINE INDENT if ( A [ i ] == '1' ) : NEW_LINE INDENT count1A += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( ( count1A & 1 ) == 1 ) : NEW_LINE INDENT odd1A += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT even1A += 1 ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( n2 ) : NEW_LINE INDENT if ( B [ i ] == '1' ) : NEW_LINE INDENT count1B += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( ( count1B & 1 ) == 1 ) : NEW_LINE INDENT odd1B += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT even1B += 1 ; NEW_LINE DEDENT DEDENT DEDENT if ( count1A == count1B and odd1A == odd1B and even1A == even1B ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = "10011" ; NEW_LINE B = "11100" ; NEW_LINE canTransformStrings ( A , B ) ; NEW_LINE DEDENT