字符串消减

来源:互联网 发布:什么软件看书好 编辑:程序博客网 时间:2024/05/11 13:47
public static int minLength( String s ) {
   int len = s.length();
   if ( s.indexOf( "a" )+s.indexOf( "b" )+s.indexOf( "c" ) == -2 ) 
       return len;
   String temp = "";
   //判断字符串是否包含a、并将其替换为bc
   if ( s.indexOf( "a" ) > -1 ){
       temp = s.replaceAll( "a" , "bc" );
       len = temp.length();
   }
   int bLen = temp.replaceAll( "b" , "" ).trim().length();
   if (len % 2 == 0 && bLen % 2 == 0) return 2;
   else return 1;
}
原创粉丝点击