您的位置:首页 > 其它

计算字符串中字符的相连字符的最大长度

2016-12-01 18:34 288 查看
package wusc.edu.test;

import java.util.HashMap;

public class StringCharCount {
public static void main(String args[]){
String str = "AAABBBDDDCCCEFGHJJUAVBBDEBV" ;
char[]  chars = str.toCharArray() ;

HashMap<String,Integer> hm = new HashMap<String,Integer>();

for(int i=0;i<chars.length;i++){
hm.put(String.valueOf(chars[i]), Integer.valueOf(1));
}

for(int j=0;j<chars.length;){

if(j==chars.length-1 ||!(chars[j+1]==chars[j])){
j++;
continue ;
}else{
int k = j ;
int count =1 ;
while(!(k==chars.length-1)&&(chars[k+1]==chars[k])){
k++;
count++;
}
if(count>hm.get(String.valueOf(chars[j]))){
hm.put(String.valueOf(chars[j]), Integer.valueOf(count));
}
j=k;
}

}

System.out.println(hm.toString());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐