/**
A string may contain multiple strings in a~z, and there may be repetitions, such as String data = "abcdaaa",
the most frequent letter in this string is a, which appears 4 times. Now that the user inputs a string, we need to find the letter with the most occurrences
and the number of times. If there is a letter with the most occurrences, it is not unique, but all the letters with the most occurrences and the number of times must be printed out.
Input: The input is a line of strings, which is the string we need to count. For example, we enter "abcabd".
Output: The output is the letter with the most occurrences and the number of occurrences, which are separated by spaces. If the letter with the most occurrences is not unique ,
print all the results in the order in which the letters appear in the string, separating the letters and times of each line with a newline
Sample input
abcabd
Sample output
a 2
b 2
sample input
babbaca
sample output
b 3
a 3
*/
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);//键盘录入
String s=sc.nextLine();
char [] chs=s.toCharArray();//将字符串转换为字符数组
Map char_int=new HashMap<Character,Integer>();//定义一个map集合
for (char c : chs) {
if(!char_int.containsKey(c)){//若集合中没有该键
int count=0;//每一个字符出现次数的计数变量
for (char ch : chs) {
if(c==ch){
count++;
//将每一个字符及该字符出现的次数以键值对的方式存储在map集合char_int里
char_int.put(c, count);
}//if
}//for
}//if
}//for
Collection<Integer> values=char_int.values();//获取集合中所有值的集合
Integer max=0;//最大值变量
for (Integer value : values) {//寻找最大值,也就是出现最多的次数
if(value>=max){
max=value;
}
}
//定义一个TreeSet集合(自动排序)
TreeSet<Integer> ts=new TreeSet<Integer>();
//获取char_int集合里所有键的集合
Set<Character> set=char_int.keySet();
for (Character key : set) {
//由键取值
Integer value=(Integer) char_int.get(key);
if(max==value){
/**
* 如果字符出现最大次数等于该键的值,
* 就把原来字符串中该键的索引存在TreeSet集合ts里,
* 由键(出现次数最多的字符)得到出现次数最多的字符 在原字符串中的索引
* 如果出现最多次数的字母不唯一,也可以按照字母在字符串中出现的顺序,
* 把所有结果打印出来,用换行符将每一行的字母和次数分开
*/
ts.add(s.indexOf((char)key));
}
}
//输出
for (Integer index : ts) {
System.out.println(chs[(int)index]+" "+max);
}
}
}