Repository navigation
Expand file tree
/
Copy pathBeecrowd1255.java
More file actions
53 lines (42 loc) · 1.44 KB
/
Copy pathBeecrowd1255.java
File metadata and controls
53 lines (42 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.*;
public class Beecrowd1255 {
public static void main(String[] args) {
HashMap<Character, Integer> map = new HashMap<>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
while (n-- > 0) {
String line = sc.nextLine();
line = line.toLowerCase();
for (int i = 0; i < line.length(); i++) {
boolean b = Character.isAlphabetic(line.charAt(i));
if (b) {
map.put(line.charAt(i), map.getOrDefault(line.charAt(i), 0) + 1);
}
}
List<Character> ch = getCharacters(map);
Collections.sort(ch);
for (Character c : ch){
System.out.print(c);
}
System.out.println();
map.clear();
}
}
private static List<Character> getCharacters(HashMap<Character, Integer> map) {
List<Integer> frequency = new ArrayList<>(map.values());
int data = frequency.get(0);
for (int i = 1; i < frequency.size(); i++) {
if (frequency.get(i) > data) {
data = frequency.get(i);
}
}
List<Character> ch = new ArrayList<>();
for(Map.Entry<Character, Integer> set : map.entrySet()){
if (set.getValue() >= data){
ch.add(set.getKey());
}
}
return ch;
}
}