[BOJ] 1157. 단어공부 - Array
제출일 : 2019-10-08
문제 풀이 시간 : 5M
난이도 : ☆
Problem
Input
첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.
Output
첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.
Example
input
Mississipi
output
?
Solution & Inpression
알파벳은 26개
Code
언어 : JAVA
메모리 : 32,220 kb
실행시간 : 332 ms
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] arr = sc.next().toUpperCase().toCharArray();
int[] cnt = new int[26];
for (int i = 0; i < arr.length; i++) {
cnt[arr[i]-'A']++;
}
int max = -1;
int maxIdx = -1;
for (int i = 0; i < cnt.length; i++) {
if(max<cnt[i]) {
max = cnt[i];
maxIdx = i;
}
}
for (int i = 0; i < cnt.length; i++) {
if(maxIdx!=i) {
if(max==cnt[i]) {
System.out.println("?");
return;
}
}
}
System.out.println((char)('A'+maxIdx));
}
}
'Problem > BOJ' 카테고리의 다른 글
[BOJ] 14889. 스타트와 링크- Permutation (0) | 2019.10.14 |
---|---|
[BOJ] 14888. 연산자끼워넣기 - Permutation (0) | 2019.10.14 |
[BOJ] 17472. 다리 만들기2 - Prim (0) | 2019.10.07 |
[BOJ] 4963. 섬의 개수 - DFS (0) | 2019.10.07 |
[BOJ] 4963. 섬의 개수 - BFS (0) | 2019.10.07 |