Rearrange a binary string as alternate x and y occurrences | Java program to arrange given string ; Function which arrange the given string ; Counting number of 0 ' s ▁ and ▁ 1' s in the given string . ; Printing first all 0 ' s ▁ x - times ▁ ▁ and ▁ decrement ▁ count ▁ of ▁ 0' s x - times and do the similar task with '1' ; Driver Code
import java . io . * ; class GFG { static void arrangeString ( String str , int x , int y ) { int count_0 = 0 ; int count_1 = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == '0' ) count_0 ++ ; else count_1 ++ ; } while ( count_0 > 0 count_1 > 0 ) { for ( int j = 0 ; j < x && count_0 > 0 ; j ++ ) { if ( count_0 > 0 ) { System . out . print ( "0" ) ; count_0 -- ; } } for ( int j = 0 ; j < y && count_1 > 0 ; j ++ ) { if ( count_1 > 0 ) { System . out . print ( "1" ) ; count_1 -- ; } } } } public static void main ( String [ ] args ) { String str = "01101101101101101000000" ; int x = 1 ; int y = 2 ; arrangeString ( str , x , y ) ; } }