public static int findMinTurns ( int n , String s ) {
  int eastTotal = 0 ;
  int westTotal = 0 ;
  for ( char ch : s . toCharArray ( ) ) {
    if ( ch == 'E' ) {
      eastTotal ++ ;
    }
    else if ( ch == 'W' ) {
      westTotal ++ ;
    }
  }
  int eastPassed = 0 ;
  int westPassed = 0 ;
  int currentTurns ;
  int leastTurns = n ;
  for ( char ch : s . toCharArray ( ) ) {
    currentTurns = westPassed + eastTotal - eastPassed ;
    if ( ch == 'E' ) {
      currentTurns -- ;
      eastPassed ++ ;
    }
    else if ( ch == 'W' ) {
      westPassed ++ ;
    }
    if ( currentTurns < leastTurns ) {
      leastTurns = currentTurns ;
    }
  }
  return leastTurns ;
}

public static int findMinTurns ( int n , String s ) { int eastTotal = 0 ; int westTotal = 0 ; for ( char ch : s . toCharArray ( ) ) { if ( ch == ' E ' ) { eastTotal ++ ; } else if ( ch == ' W ' ) { westTotal ++ ; } } int eastPassed = 0 ; int westPassed = 0 ; int currentTurns ; int leastTurns = n ; for ( char ch : s . toCharArray ( ) ) { currentTurns = westPassed + eastTotal - eastPassed ; if ( ch == ' E ' ) { currentTurns -- ; eastPassed ++ ; } else if ( ch == ' W ' ) { westPassed ++ ; } if ( currentTurns < leastTurns ) { leastTurns = currentTurns ; } } return leastTurns ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (integral_type) name: (identifier)) (formal_parameter type: (type_identifier) 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))) (enhanced_for_statement type: (integral_type) name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (character_literal))) consequence: (block (expression_statement (update_expression (identifier)))) alternative: (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (character_literal))) consequence: (block (expression_statement (update_expression (identifier)))))))) (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))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (enhanced_for_statement type: (integral_type) name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) body: (block (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier)))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (character_literal))) consequence: (block (expression_statement (update_expression (identifier))) (expression_statement (update_expression (identifier)))) alternative: (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (character_literal))) consequence: (block (expression_statement (update_expression (identifier)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (identifier))))))) (return_statement (identifier))))