public static int solve ( String str ) {
  int ans , i , j ;
  i = 0 ;
  j = str . length ( ) - 1 ;
  while ( true ) {
    ans = 1 ;
    if ( str . charAt ( i ) != str . charAt ( j ) || i == j ) return - 1 ;
    while ( i < str . length ( ) - 1 ) {
      if ( str . charAt ( i ++ ) != str . charAt ( i ) ) break ;
      ans ++ ;
    }
    if ( i >= j ) return ans ;
    while ( j >= 1 ) {
      if ( str . charAt ( j -- ) != str . charAt ( j ) ) break ;
      ans ++ ;
    }
    if ( ans < 2 ) return - 1 ;
  }
}

public static int solve ( String str ) { int ans , i , j ; i = 0 ; j = str . length ( ) - 1 ; while ( true ) { ans = 1 ; if ( str . charAt ( i ) != str . charAt ( j ) || i == j ) return - 1 ; while ( i < str . length ( ) - 1 ) { if ( str . charAt ( i ++ ) != str . charAt ( i ) ) break ; ans ++ ; } if ( i >= j ) return ans ; while ( j >= 1 ) { if ( str . charAt ( j -- ) != str . charAt ( j ) ) break ; ans ++ ; } if ( ans < 2 ) return - 1 ; } }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier)) declarator: (variable_declarator name: (identifier)) declarator: (variable_declarator name: (identifier))) (expression_statement (assignment_expression left: (identifier) right: (decimal_integer_literal))) (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) (while_statement condition: (parenthesized_expression (true)) body: (block (expression_statement (assignment_expression left: (identifier) right: (decimal_integer_literal))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) right: (binary_expression left: (identifier) right: (identifier)))) consequence: (return_statement (unary_expression operand: (decimal_integer_literal)))) (while_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (update_expression (identifier)))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))) consequence: (break_statement)) (expression_statement (update_expression (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (return_statement (identifier))) (while_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (update_expression (identifier)))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))) consequence: (break_statement)) (expression_statement (update_expression (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (return_statement (unary_expression operand: (decimal_integer_literal))))))))