ALGORITHM/프로그래머스 | 백준 | 삼성 | 카카오

[백준] 1157번 단어 공부

SZCODE 2020. 8. 23. 20:41

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);

	}
}