text: Check if all substrings of length K of a Binary String has equal count of 0 s and 1 s | Java program for the above approach ; Function to check if the substring of length K has equal 0 and 1 ; Traverse the String ; Check if every K - th character is the same or not ; Traverse subString of length K ; If current character is 0 ; Increment count ; Otherwise ; Decrement count ; Check for equal 0 s and 1 s ; Driver code
code: import java . util . * ; class GFG { static boolean check ( String s , int k ) { int n = s . length ( ) ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = i ; j < n ; j += k ) { if ( s . charAt ( i ) != s . charAt ( j ) ) return false ; } } int c = 0 ; for ( int i = 0 ; i < k ; i ++ ) { if ( s . charAt ( i ) == '0' ) c ++ ; else c -- ; } if ( c == 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { String s = "101010" ; int k = 2 ; if ( check ( s , k ) ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }