코딩테스트
[백준] 1371번 가장 많은 글자 (사용언어 : JAVA)
김토끼A
2021. 8. 14. 21:29
문제
영어에서는 어떤 글자가 다른 글자보다 많이 쓰인다. 예를 들어, 긴 글에서 약 12.31% 글자는 e이다.
어떤 글이 주어졌을 때, 가장 많이 나온 글자를 출력하는 프로그램을 작성하시오.
입력
첫째 줄부터 글의 문장이 주어진다. 글은 최대 5000글자로 구성되어 있고, 공백, 알파벳 소문자, 엔터로만 이루어져 있다. 그리고 적어도 하나의 알파벳이 있다.
출력
첫째 줄에 가장 많이 나온 문자를 출력한다. 여러 개일 경우에는 알파벳 순으로 앞서는 것부터 모두 공백없이 출력한다.
예제
예제 입력1 | 예제 출력1 |
english is a west germanic language originating in england and is the first language for most people in the united kingdom the united states canada australia new zealand ireland and the anglophone caribbean it is used extensively as a second language and as an official language throughout the world especially in common wealth countries and in many international organizations |
a |
예제 입력2 | 예제 출력2 |
baekjoon online judge | eno |
구현
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
String str;
int alpha[] = new int[26];
int max = 0;
while((str = bufferedReader.readLine()) != null) {
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) != ' '){
alpha[str.charAt(i) - 'a']++;
if(max < alpha[str.charAt(i) - 'a']){
max = alpha[str.charAt(i) - 'a'];
}
}
}
}
for(int i = 0; i < 26; i++){
if(alpha[i] == max) bufferedWriter.write((char)(i + 'a'));
}
bufferedReader.close();
bufferedWriter.close();
}
}
1371번: 가장 많은 글자
첫째 줄부터 글의 문장이 주어진다. 글은 최대 5000글자로 구성되어 있고, 공백, 알파벳 소문자, 엔터로만 이루어져 있다. 그리고 적어도 하나의 알파벳이 있다.
www.acmicpc.net