给你一个字符串例如: "kaaabzzyyyy"
找出字符串中一样的字符连续并且是最多的并算出它的个数
如字符串 "kaaabzzyyyy"  yyyy最多 连续出现了4次
就返回 (y,4)
如给出字符串如 "kaaabbbzzy"
aaa bbb 都出现了3次 知返回先出现的(a,3)
字符串 长度为(1 - 40)
是(a -z)之间的
定义 public String UnitConvert(String aaa)

解决方案 »

  1.   

    int getMaxCount(String strDemo, String strItem) {
        StringTokenizer st = new StringTokenizer(strDemo, strItem ,true);
        int i = 0;
        final int nLen = st.countTokens();
        int nCurrCount = 0;
        int nMax = 0;
        String[] strResult = new String[nLen];
        while (st.hasMoreTokens()) {
    strResult[i] = st.nextToken();
    if (strResult[i].equals(strItem)) {
       ++nCurrCount;
    } else {
       if (nCurrCount>nMax)
        nMax = nCurrCount;
      nCurrCount = 0;
    }
    ++i;
       }
       return nMax;
     }String  strDemo = "kabzzyyaaaayy";
    String strItem = "";
    char [] chArray = strDemo.toCharArray();
    HashMap  hpResult = new HashMap();
    for (int i =0 ;i < chArray.length ; ++i) {
      System.out.println("Begin------------------------------------------------------");
      strItem = ""+ chArray[i];
      if (!hpResult.containsKey(strItem))
        hpResult.put(strItem , ""+getMaxCount(strDemo,strItem));
    }
    //结果存在hpResult中
      

  2.   

    解决方法很多,不过思想都差不多,星空的就应该没问题
    www.topcoder.com上面有很多这样的题目的
      

  3.   

    public String UnitConvert(String str){
      char c[] = str.toCharArray();
      char cpre = c[0];
      int endPos = 0;
      int count = 1;
      int max = 0;
      
      for(int i=1;i<c.length;i++){
        if(c[i] == cpre){
          count++;
        }else{
          if(count>max){
            endPos = i;
            max = count;
          }
          count=1;
          cpre = c[i];
        }
      }  return str.substring(endPos-max,endPos);
    }
      

  4.   

    public class UnitConvert {public String findUnitConvert(String aaa){
    int index = 0, times = 1, maxTimes = 0;
    char[] ccc = aaa.toCharArray();
    char myChar = 0, maxChar = 0;while(index < aaa.length()){
    if(myChar == ccc[index]){
    times++;
    }
    else{
    myChar = ccc[index];
    times = 1;
    }
    if(maxTimes < times){
    maxTimes = times;
    maxChar = myChar;
    }
    index++;
    }
    return "(" + maxChar + ", " + maxTimes + ")";
    }public static void main(String[] args){
    UnitConvert uc = new UnitConvert();
    System.out.println(uc.findUnitConvert("kaaabzzyyyy"));
    }
    }
      

  5.   

    Why put it again !?