今日于csdn上看到一个问题:
个人解决方法:
import java.util.ArrayList;
import java.util.List;
public class removeChar {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String arg = "cabababbabcabaaaababacabab";
arg = removeStr(arg);
System.out.println(arg);
int minLength = arg.length();
System.out.println("最短长度是:"+minLength);
return;
}
static String removeStr(String arg){
String[] temp = arg.split("");
List<String> tempList = new ArrayList<String>();
for(String tempAA:temp){
if(!tempAA.equals("")){
tempList.add(tempAA);
}
}
for(int i=0;i<tempList.size()-1;i++){
if(i==tempList.size()-1){
break;
}
if(tempList.get(i).equals("a")){
if(tempList.get(i+1).equals("b")){
tempList.set(i, "c");
tempList.remove(i+1);
}else if(tempList.get(i+1).equals("c")){
tempList.set(i, "b");
tempList.remove(i+1);
}
}else if(tempList.get(i).equals("b")){
if(tempList.get(i+1).equals("c")){
tempList.set(i, "a");
tempList.remove(i+1);
}else if(tempList.get(i+1).equals("a")){
tempList.set(i, "c");
tempList.remove(i+1);
}
}else if(tempList.get(i).equals("c")){
if(tempList.get(i+1).equals("a")){
tempList.set(i, "b");
tempList.remove(i+1);
}else if(tempList.get(i+1).equals("b")){
tempList.set(i, "a");
tempList.remove(i+1);
}
}
}
String newArg="";
for(String tempBB:tempList){
newArg += tempBB;
}
for(String tempCC:tempList){
if(!tempCC.equals(tempList.get(0))){
newArg=removeStr(newArg);
break;
}
}
return newArg;
}
}
大家帮忙看看对不,有没有更好的方法解决啊!!!