Check if binary string multiple of 3 using DFA | Java program to check if the binary String is divisible by 3. ; 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
import java . util . * ; class GFG { static void CheckDivisibilty ( String A ) { int oddbits = 0 , evenbits = 0 ; for ( int counter = 0 ; counter < A . length ( ) ; counter ++ ) { if ( A . charAt ( counter ) == '1' ) { if ( counter % 2 == 0 ) { evenbits ++ ; } else { oddbits ++ ; } } } if ( Math . abs ( oddbits - evenbits ) % 3 == 0 ) { System . out . print ( " Yes " + "NEW_LINE"); } else { System . out . print ( " No " + "NEW_LINE"); } } public static void main ( String [ ] args ) { String A = "10101" ; CheckDivisibilty ( A ) ; } }