반응형
    
    
    
  문제

풀이
대문자 A ~ Z 알파벳 개수를 세기 위한 26개 배열을 만들고
char형 'A'의 값의 int형 값은 65
입력받은 문자열의 문자를 char형 → int형으로 변환해서 해당 배열에 인덱스의 값을 +1을 함
코드
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int[] strCount = new int[26]; //A ~ Z 알파벳 26개
        int max = 0;
        int maxIndex = 0;
        str = str.toUpperCase();
        
        //알파벳 수 계산
        for (int i = 0; i < str.length(); i++) {
            strCount[(int)str.charAt(i) - 65]++; // char A = 65
        }
        
        //가장 많이 사용된 알파벳 찾기
        for (int i = 0; i < strCount.length; i++) {
            if (max < strCount[i]) {
                max = strCount[i];
                maxIndex = i;
            } else if (max == strCount[i]) {
                maxIndex = -1;
            }
        }
        if (maxIndex == -1) {
            System.out.println("?");
        } else {
            System.out.println((char)(maxIndex + 65));
        }
    }
}
출처
반응형
    
    
    
  '알고리즘 > 백준' 카테고리의 다른 글
| 백준 18870번 좌표 압축 자바 java (1) | 2021.04.20 | 
|---|---|
| 백준 1011번 Fly me to the Alpha Centauri 자바 java (1) | 2021.04.14 | 
| 백준 2869번 달팽이는 올라가고 싶다 자바 java (1) | 2021.04.13 | 
| 백준 1065번 한수 자바 java (0) | 2021.04.12 | 
| 백준 4673번 셀프 넘버 자바 java (1) | 2021.04.11 | 
														
													
														
													
														
													
														
													
댓글