You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package class022 ;
public class Code01_MinSwapStep {
public static int minSteps1 ( String s ) {
if ( s = = null | | s . equals ( "" ) ) {
return 0 ;
}
char [ ] str = s . toCharArray ( ) ;
int step1 = 0 ;
int gi = 0 ;
// 想让G都放在左侧, 请问至少需要交换几次, 需要算出step1来。
for ( int i = 0 ; i < str . length ; i + + ) {
if ( str [ i ] = = 'G' ) {
step1 + = i - ( gi + + ) ;
}
}
int step2 = 0 ;
int bi = 0 ;
for ( int i = 0 ; i < str . length ; i + + ) {
if ( str [ i ] = = 'B' ) {
step2 + = i - ( bi + + ) ;
}
}
return Math . min ( step1 , step2 ) ;
}
public static int minSteps ( String s ) {
if ( s = = null | | s . equals ( "" ) ) {
return 0 ;
}
char [ ] str = s . toCharArray ( ) ;
int step1 = 0 ;
int step2 = 0 ;
int gi = 0 ;
int bi = 0 ;
for ( int i = 0 ; i < str . length ; i + + ) {
if ( str [ i ] = = 'G' ) {
step1 + = i - ( gi + + ) ;
} else {
step2 + = i - ( bi + + ) ;
}
}
return Math . min ( step1 , step2 ) ;
}
public static void main ( String [ ] args ) {
String s = "BGGBB" ;
System . out . println ( minSteps ( s ) ) ;
}
}