Flip the substrings of size K starting from i
for ( int j = i ; j < i + K ; j ++ ) { if ( S . charAt ( j ) == '1' ) S = S . substring ( 0 , j ) + '0' + S . substring ( j + 1 ) ; else S = S . substring ( 0 , j ) + '1' + S . substring ( j + 1 ) ; }