본문 바로가기

알고리즘/Boj

[JAVA] 백준 1157 - 단어 공부

 

 

#flaticon

 

 

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine().toUpperCase();
		
		char[] arr = new char[200];
		int max = 0;
		
		// 문자열의 길이만큼 반복문 수행하며 최대값을 구함
		for(int i=0; i<s.length(); i++) {
			arr[s.charAt(i)]++;
			max = Math.max(max, arr[s.charAt(i)]);
		}
		
		// 최대값이랑 같을 경우 ArrayList에 추가
		ArrayList<Character> al = new ArrayList<>();
		for(int i=0; i<arr.length; i++) {
			if(arr[i] == max) al.add((char)i);
		}
		
		// 1개일 경우 출력하고, 여러개일 경우 ? 출력
		if(al.size() == 1)
			System.out.println(al.get(0));
		else
			System.out.println("?");
	}
}