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());
}
}
计算字符串中字符的相连字符的最大长度
最新推荐文章于 2024-05-15 19:50:37 发布
