핑구

[백준/JAVA] 1157번 : 단어 공부 본문

CS/알고리즘

[백준/JAVA] 1157번 : 단어 공부

코딩 펭귄 2021. 9. 7. 13:54

https://www.acmicpc.net/problem/1157

 

1157번: 단어 공부

알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.

www.acmicpc.net

 

 

해당 문제는 알파벳 대소문자를 구분하지 않으며, 출력은 대문자로 하기 때문에 입력을 받은 문자열을 모두 대문자로 변환한 후 진행하였습니다.

 

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine().toUpperCase();
		char[] ch = {str.charAt(0)};
		
		
		for(int i = 0; i < str.length(); i++) {
			boolean flag = true;
			for(int j = 0; j < ch.length; j++) {
				if(ch[j] == str.charAt(i)) {
					flag = false;
					break;
				}
			}
			if(flag) {
				ch = Arrays.copyOf(ch, ch.length+1);
				ch[ch.length-1] = str.charAt(i);
			}
		}
		
		int[] count = new int[ch.length];
		int max = 0;
		
		for(int i = 0; i < ch.length; i++) {
			for(int j = 0; j < str.length(); j++) {
				if(ch[i] == str.charAt(j)) {
					count[i]++;
				}
			}
			if(max < count[i]) {
				max = count[i];
			}
		}
		
		int dup = 0;
		int result = 0;
		for(int i = 0; i < count.length; i++) {
			if(max == count[i]) {
				dup++;
				result = i;
			}
		}
		
		if(dup > 1) {
			System.out.println("?");
		} else {
			System.out.println(ch[result]);
		}
	}
}

'CS > 알고리즘' 카테고리의 다른 글

[백준/JAVA] 2839번 : 설탕 배달  (0) 2021.09.07
[백준/JAVA] 1152번 : 단어의 개수  (0) 2021.09.07
[백준/JAVA] 1008번 : A/B  (0) 2021.09.07
[백준/JAVA] 8958번 : OX 퀴즈  (0) 2021.09.07
[백준/JAVA] 1546번 : 평균  (0) 2021.09.07