백준 알고리즘 - 1157 (단어 공부)

aladin·2020년 8월 20일
0

백준알고리즘

목록 보기
6/18

문제

I.O

코드 및 해석

import java.util.*;

public class Boj_1157 {

	public static void main(String[] args) {
		int[] cntAlpha = new int[26];
		int max = 0;
		int findIndex = 0;
		
		Scanner sc = new Scanner(System.in);
		String inputStr = sc.nextLine();
		
		for(int i = 0 ; i < inputStr.length() ; i++) {
			char charAt = inputStr.charAt(i);
			
			if(charAt >= 97) {
				cntAlpha[charAt - 32 - 65]++;		//소문자면 32 빼주고 65빼줌
			} else {
				cntAlpha[charAt - 65]++;
			}
		}
		
		for(int i = 0 ; i < cntAlpha.length ; i++) {
			if(max < cntAlpha[i]) {
				max = cntAlpha[i];
				findIndex = i;
			}
		}
		int cnt = 0;
		
		for(int i = 0 ; i < cntAlpha.length ; i++) {
			if(cntAlpha[i] == max) cnt++;
		}
		char findChar = (char) (findIndex + 65);
		
		if(cnt >= 2) System.out.println("?");
		else System.out.println(findChar);
	}
}

문제 및 사진출처

출처 - 백준 알고리즘_1157번

profile
컴공과 대학생의 개발노트

0개의 댓글