Problem Solving/Baekjoon
[백준] 9612 Maximum Word Frequency - Data Structure / Java
graycode
2023. 5. 3. 21:15
• 문제 링크
9612번: Maximum Word Frequency
Print out the word that has the highest frequency and its frequency, separated by a single space. If you get more than 2 results, choose only the one that comes later in the lexicographical order.
www.acmicpc.net
• 풀이 과정
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
Map<String, Integer> map = new HashMap<>();
while (n-- > 0) {
String key = br.readLine();
map.put(key, map.getOrDefault(key, 0) + 1);
}
List<String> list = new ArrayList<>(map.keySet());
list.sort(Comparator.reverseOrder());
String word = null;
int max = 0;
for (String key : list) {
int val = map.get(key);
if (val > max) {
max = val;
word = key;
}
}
bw.write(word + " " + max);
bw.flush();
}
}