[백준] 1157

당당·2023년 4월 21일
0

백준

목록 보기
35/179

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

📔문제

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


📝입력

첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.


📺출력

첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.


📝예제 입력 1

Mississipi

📺예제 출력 1

?

📝예제 입력 2

zZa

📺예제 출력 2

Z

📝예제 입력 3

z

📺예제 출력 3

Z

📝예제 입력 4

baaa

📺예제 출력 4

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;
//		}
		

		
		
	}

}

📰출력 결과


📂고찰

https://velog.io/@dangdang/백준-10809

이거때처럼 -'A'로 계산했으면
char배열은 안썼어도 됐을거같다..

심화 1까진 클리어!!

profile
MySQL DBA 신입 지원

0개의 댓글