알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳
이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.
첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?
를 출력한다.
Mississipi
?
zZa
Z
z
Z
baaa
A
-문제를 만든 사람: author5
-데이터를 추가한 사람: jh05013, rnjs4197
import java.util.Scanner;
public class Code1157 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
String word=scanner.next();
word=word.toUpperCase(); //change upper
int len=word.length();
int max=0;
char[] alpha={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int[] count=new int[26];
char temp='0';
for(int j=0;j<len;j++) {
for(int i=0;i<26;i++) {
if(word.charAt(j)==alpha[i]) {
count[i]=count[i]+1;
}
}
}
for(int i=0;i<26;i++) {
if(max<count[i]) {
max=count[i];
temp=alpha[i];
}
else if(max==count[i]) {
temp='?';
}
}
System.out.println(temp);
// char temp;
//
// int len=wordc.length;
//
// int max=0;
// int count=0;
// int maxcount=0;
// char temps='0';
// for(int i=0;i<len;i++) { //sort word
// for(int j=i;j<len;j++) {
// if(wordc[i]<wordc[j]) {
// temp=wordc[i];
// wordc[i]=wordc[j];
// wordc[j]=temp;
// }
// }
// }
//
// for(int i=0;i<len;i++) {
// count=0;
// for(int j=i;j<len;j++) {
// if(wordc[i]==wordc[j]) {
// count++;
// }
// else {
// break;
// }
// }
// if(max<count) {
// max=count;
// temps=wordc[i];
// }
// else if(max==count) {
// temps='?';
// break;
// }
// i=i+count-1;
// }
}
}
이거때처럼 -'A'
로 계산했으면
char배열
은 안썼어도 됐을거같다..
심화 1까진 클리어!!