Check if binary string multiple of 3 using DFA | Function to check if the binary String is divisible by 3. ; checking if the bit is nonzero ; checking if the nonzero bit is at even position ; Checking if the difference of non - zero oddbits and evenbits is divisible by 3. ; Driver Program
def CheckDivisibilty ( A ) : NEW_LINE INDENT oddbits = 0 ; NEW_LINE evenbits = 0 ; NEW_LINE for counter in range ( len ( A ) ) : NEW_LINE INDENT if ( A [ counter ] == '1' ) : NEW_LINE INDENT if ( counter % 2 == 0 ) : NEW_LINE INDENT evenbits += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT oddbits += 1 ; NEW_LINE DEDENT DEDENT DEDENT if ( abs ( oddbits - evenbits ) % 3 == 0 ) : 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 = "10101" ; NEW_LINE CheckDivisibilty ( A ) ; NEW_LINE DEDENT