【发布时间】:2018-10-23 09:54:29
【问题描述】:
我想获得最大重复字符数及其相关索引。我能够打印给定字符串及其索引中的最大重复字符。但是我无法打印重复字符的总数。下面是我的代码
public class MaxRepeating {
static char charactercountIndex(String str) {
int len = str.length();
int count = 0;
char res = str.charAt(0);
for (int i = 0; i < len; i++) {
int cur_count = 0;
for (int j = i + 1; j < len; j++) {
if (str.charAt(i) != str.charAt(j))
break;
cur_count++;
}
if (cur_count > count) {
count = cur_count;
res = str.charAt(i);
}
}
return res;
}
public static void main(String args[]) {
String str = "aaaaaaccde";
char s1 = charactercountIndex(str);
str.indexOf(s1);
System.out.println(str.indexOf(s1));
System.out.println(charactercountIndex(str));
}
}
输出应该 0 是字符 a 的索引 6 是字符串中出现的总时间字符“a”
【问题讨论】:
-
我怀疑您的索引是否正确。你可以google一下问题。网上有多种解决方案
-
我得到了正确的索引,但是我不能得到字符的实际计数(在这种情况下是“a”,应该是 6)