```
백준 9612번 Maximum Word Frequency JAVA 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 9612번 풀이
https://www.acmicpc.net/problem/9612
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
백준 9612번 Maximum Word Frequency는
난이도 실버 등급의 문제로서
1개부터 최대 1000개까지 단어들을 입력받고
해당되는 단어 중에
가장 빈도가 높은 단어의 빈도수와 단어를 출력해주면 되는 문제입니다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
맵을 이용 해서
해당되는 단어의 빈도수를 업데이트하고
가장 빈도수가 높은 단어의 빈도수와 단어를 출력했습니다
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Map<String, Integer> wordCountMap = new HashMap<>();
final int testCase = Integer.parseInt(br.readLine());
for (int i = 0; i < testCase; i++) {
final String input = br.readLine();
if (wordCountMap.containsKey(input)) {
int count = wordCountMap.get(input) + 1;
wordCountMap.replace(input, count);
} else {
wordCountMap.put(input, 1);
}
}
Map.Entry<String, Integer> maxEntry = Collections.max(wordCountMap.entrySet(), Map.Entry.comparingByValue());
System.out.print(maxEntry.getKey() + " " + maxEntry.getValue());
}
}
// https://codemasterkimc.tistory.com
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
728x90
'Java > Java 알고리즘' 카테고리의 다른 글
| 백준 2204번 도비의 난독증 테스트 JAVA 구현해보기 (1) | 2022.06.06 |
|---|---|
| 백준 4493번 가위 바위 보? JAVA 구현해보기 (0) | 2022.06.05 |
| 백준 9506번 약수들의 합 JAVA 구현해보기 (0) | 2022.06.05 |
| 백준 9610번 사분면 JAVA 구현해보기 (0) | 2022.06.05 |
| 백준 4447번 좋은놈 나쁜놈 JAVA 구현해보기 (0) | 2022.06.05 |