日期:2014-05-20  浏览次数:20682 次

java求教 简单的 有点不懂
从控制台中输入一个字符串,并输出连续出现次数最多的字符串
比如啊abbbbccccccc输出ccccccc;

------解决方案--------------------
Java code

public class StringMost {
    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public  String getMostString(String str2) {
        
        String result="";
        String result1="";
        while(str2.length()>1){
            result1="";
            result1 = t(str2,result1,0);
            if(result.length()<result1.length()){
                result = result1;
                
            }
            str2 = str2.substring(result1.length());
            if(str2.length()==0){
                break;
            }
            result1="";
            result1 = t(str2,result1,0);
            str2 = str2.substring(result1.length());
        
            if(result.length()<result1.length()){
                result = result1;
                
            }
        }
        
        return result;
    }

    private String t(String str2, String result, int i) {
        if(str.length()==0){
            return result;
        }
        if(i>=(str2.length()-1)){
            result = result+str2.charAt(i);
            return result;
        }
        result = result+str2.charAt(i);    
        if(str2.charAt(i)!=str2.charAt(i+1)){
            return result;
        }
        else{
        result = t(str2,result,i+1);
        }
        return result;
    }
}