试图找出为什么我的双字计数器没有正确排序我的数组并保持正确的匹配计数。非常感谢帮助。
示例 - > "的 DL KJGJFF的 DL JD的 DL JL的 DL FK" 答案 - > DL = 4
int cipherTxt_length = cipherTxt.length();
String ch;
int count=0;
for(char i='a'; i<='z'; i++)
{
System.out.println("inside first for loop -- count -- "+count);
for(char k='a'; k<='z'; k++)
{
System.out.println("inside first for loop -- count -- "+count);
count = 0;
for(int j=0; j<cipherTxt_length; j++)
{
System.out.println("inside first for loop -- count -- "+count);
ch=cipherTxt.substring(j,j+2); //extracting characters of the string one by one
if(ch.charAt(j)==k && ch.charAt(j+2)==i) //first checking the whole string for 'a', then 'b' and so on
count++; //increasing count of those aplhabets which are present in the string
}
if(count!=0)//printing only those aphabets whose count is not '0'
{
System.out.println(i+"\t\t"+count);//\t is tabbing method
}
}
}
}
&#13;
答案 0 :(得分:0)
试试这个
String s = "DLKJGJFFDLJDDLJLDLFK";
Map<String, Integer> map = IntStream.range(0, s.length() - 1)
.mapToObj(i -> s.substring(i, i + 2))
.collect(HashMap::new, (m, e) -> m.compute(e, (k, v) -> v == null ? 1 : v + 1), (m, n) -> m.putAll(n));
Entry<String, Integer> max = map.entrySet().stream()
.max(Comparator.comparing(Entry::getValue)).get();
System.out.println(max); // -> DL=4