15238번: Pirates
문제 출처 : https://www.acmicpc.net/problem/15238
언어 : Kotlin
문제 설명 :
Pirates talk a funny way. They say word where the letters are repeated more than they need to be. We would like know which letter appears the most frequently in a Pirate word.
For example: In the word “arrrrrghh”, the letter “r” appears 5 times while “h” appears twice.
Write a program that reads a pirate word from the terminal and writes the letter that occurs most frequently.
It is guaranteed that only one letter is the most repeated. That is, words like “aipo” won’t appear because no single letter wins.
입력 :
The input will consist of two lines.
The first line will contain the size of the word and the second the word to process.
The word will only contain lowercase letters from a to z. The size of the word will be at most 1000 characters. No spaces or other symbols will appear.
출력 :
Print a single line with the character that appears the most and the number of occurrences.
제한 사항 :
- 시간 제한 : 2초
- 메모리 제한 : 512MB
입출력 예 :
입력 | 출력 |
10 arrrrrrghh |
r 6 |
9 shhhiiver |
h 3 |
20 oxlbelhfikrzxcgxpfcy |
x 3 |
풀이 :
import java.io.BufferedWriter
import java.io.OutputStreamWriter
fun main() = with(System.`in`.bufferedReader()) {
val bw = BufferedWriter(OutputStreamWriter(System.out))
val n = readLine().toInt()
readLine().groupingBy { it }.eachCount().maxBy { it.value }.toPair().let {
bw.write("${it.first} ${it.second}")
}
bw.flush()
bw.close()
}