static int countSubstring ( String S , int n ) {
  int ans = 0 ;
  int i = 0 ;
  while ( i < n ) {
    int cnt0 = 0 , cnt1 = 0 ;
    if ( S . charAt ( i ) == '0' ) {
      while ( i < n && S . charAt ( i ) == '0' ) {
        cnt0 ++ ;
        i ++ ;
      }
      int j = i ;
      while ( j < n && S . charAt ( j ) == '1' ) {
        cnt1 ++ ;
        j ++ ;
      }
    }
    else {
      while ( i < n && S . charAt ( i ) == '1' ) {
        cnt1 ++ ;
        i ++ ;
      }
      int j = i ;
      while ( j < n && S . charAt ( j ) == '0' ) {
        cnt0 ++ ;
        j ++ ;
      }
    }
    ans += Math . min ( cnt0 , cnt1 ) ;
  }
  return ans ;
}

static int countSubstring ( String S , int n ) { int ans = 0 ; int i = 0 ; while ( i < n ) { int cnt0 = 0 , cnt1 = 0 ; if ( S . charAt ( i ) == '0' ) { while ( i < n && S . charAt ( i ) == '0' ) { cnt0 ++ ; i ++ ; } int j = i ; while ( j < n && S . charAt ( j ) == '1' ) { cnt1 ++ ; j ++ ; } } else { while ( i < n && S . charAt ( i ) == '1' ) { cnt1 ++ ; i ++ ; } int j = i ; while ( j < n && S . charAt ( j ) == '0' ) { cnt0 ++ ; j ++ ; } } ans += Math . min ( cnt0 , cnt1 ) ; } return ans ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (while_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal)) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal))) consequence: (block (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal)))) body: (block (expression_statement (update_expression (identifier))) (expression_statement (update_expression (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal)))) body: (block (expression_statement (update_expression (identifier))) (expression_statement (update_expression (identifier)))))) alternative: (block (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal)))) body: (block (expression_statement (update_expression (identifier))) (expression_statement (update_expression (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal)))) body: (block (expression_statement (update_expression (identifier))) (expression_statement (update_expression (identifier))))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier))))))) (return_statement (identifier))))