코테/백준
백준 1157 단어공부 자바
65살까지 코딩
2022. 1. 20. 09:51
728x90
반응형
import java.io.*;
import java.util.ArrayList;
import java.util.List;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//String 하나를 입력받고
String s = br.readLine();
//소대문자 구분없이 세고 출력이 대문자기 때문에 대문자로 바꿔줌
s = s.toUpperCase();
char[] chars = s.toCharArray();
int[] count = new int[s.length()];
List<Character> list = new ArrayList<>();
//String을 char배열로 바꾸어 반복문을 돌면서 몇개인지 카운트
for(int i =0; i <chars.length-1; i++) {
//이미 센 알파벳이라면 세지 않음
if(!list.contains(chars[i])) {
for (int j = i+1; j < chars.length; j++) {
if (chars[i] == chars[j]) {
count[i]++;
}
}
}
//센 알파벳을 저장
list.add(chars[i]);
}
int max = 0;
int tmp =0;
// count의 맥스값과 index값 찾기
for(int i =0; i<count.length; i++){
if(count[i] > max){
max = count[i];
tmp = i;
}
}
//max가 tmp가 아니고 다른곳에서 같다면 max값이 2이상이므로 ?출력
for(int i =0; i<count.length; i++){
if(count[i] ==max && i !=tmp){
System.out.println("?");
return;
}
}
//그렇지 않다면 max값의 인덱스인 tmp에 있는 알파벳 출력
System.out.println(chars[tmp]);
}
}
728x90
반응형