Coding Test/오답노트
[백준 #1157] 단어공부 (java)
Serina_Heo
2022. 3. 7. 15:37
https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] arr = new int[26];
// 1. 모두 대문자로 변경
String text = br.readLine().toUpperCase();
int max = -1;
char st = '?';
for(int i=0; i<text.length(); i++) {
// 2. 아스키코드를 이용
arr[text.charAt(i)-65]++;
if(max < arr[text.charAt(i)-65]) {
max = arr[text.charAt(i)-65];
st = text.charAt(i);
} else if (max == arr[text.charAt(i)-65])
st = '?';
}
System.out.println(st);
}
}
풀이를 보면 어렵지 않은데, 문제를 풀 때면 풀이 방법이 잘 떠오르지 않는다 ㅠ