• 문제 링크
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();
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 25644 최대 상승 - Greedy / Java (0) | 2023.05.05 |
---|---|
[백준] 3022 PRASE - Data Structure / Java (0) | 2023.05.04 |
[백준] 24511 queuestack - Data Structure / Java (0) | 2023.05.02 |
[백준] 9872 Record Keeping - Data Structure / Java (0) | 2023.05.01 |
[백준] 5464 주차장 - Data Structure / Java (0) | 2023.04.30 |
댓글