문제 링크 : https://www.acmicpc.net/problem/1157
입력 받은 String 에서 가장 많이 사용되는 알파벳을 출력하는 문제입니다.
만약 가장 많이 사용된 알파벳의 수가 동일하다면 ? 을 출력합니다.
알파벳 갯수만큼 사이즈 26인 배열 arr 을 선언하여 알파벳의 갯수를 담아둡니다.
max값보다 크다면 새로운 알파벳을 answer 값에 갱신해주고 만약 max 값과 똑같다면 '?' 값을 answer에 입력합니다.
idx 값은 소문자에서 -'a' 를 해주면 인덱스만큼 구할 수 있습니다.
이후에 값을 넣을 때는 대문자로 출력해야 하기 때문에 i+65 값을 바꿔주면 됩니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
// https://www.acmicpc.net/problem/1157 | |
class Main { | |
public static void main(String[] args) throws Exception { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String s = br.readLine(); | |
int[] arr = new int[26]; | |
int len = s.length(); | |
// 각 글자 갯수를 arr 배열에 저장 | |
for(int i=0; i<len; i++) { | |
int idx = Character.toLowerCase(s.charAt(i)) - 'a'; | |
arr[idx]++; | |
} | |
int max = -1; | |
char answer = '?'; | |
for(int i=0; i<26; i++) { | |
if(arr[i] > max) { | |
max = arr[i]; | |
answer = (char) (i+65); | |
} else if(arr[i] == max) | |
answer = '?'; | |
} | |
System.out.println(answer); | |
} | |
} |
'알고리즘 문제 > 백준' 카테고리의 다른 글
백준 2438번. 별 찍기 - 1 (Java) (0) | 2019.01.05 |
---|---|
백준 1924번. 2007년 (Java) (0) | 2019.01.05 |
백준 9466번. 텀 프로젝트 (Java) (1) | 2018.12.31 |
백준 2075번. N 번째 큰 수 (Java) (0) | 2018.12.29 |
백준 1005번. ACM Craft (Java) (0) | 2018.12.28 |