https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
int[] arr = new int[26];
s = s.toUpperCase();//대문자로 변환
for (int i = 0; i < s.length(); i++) {
arr[s.charAt(i)-65]++;
}
int max = Integer.MIN_VALUE;
char answer = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]> max) {
max = arr[i];
answer = (char)(i+65);
}else if(max == arr[i]) {//가장 큰 수가 여러 개 존재하는 경우
answer = '?';
}
}
System.out.println(answer);
}
}
'ALGORITHM > 프로그래머스 | 백준 | 삼성 | 카카오' 카테고리의 다른 글
[프로그래머스] 2019 카카오 개발자 겨울 인턴십 크레인 인형뽑기 게임 (0) | 2020.08.25 |
---|---|
[백준] 1764번 듣보잡 (0) | 2020.08.23 |
[백준] 10808번 알파벳 개수 (0) | 2020.08.23 |
[백준] 2902번 KMP는 왜 KMP일까? (0) | 2020.08.23 |
[백준] 2589번 보물섬 (0) | 2020.08.19 |