(수근수근)

[백준] 단어공부 본문

Algorithm

[백준] 단어공부

InformationFarm 2020. 5. 26. 17:33

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

 

Mississipi  출력값 ?

zZa 출력값 Z

baaa 출력값 A

 

처음에 괜히 어렵게 생각해서 hashmap 자료구조 써서 풀었다......

메모리 초과 당연히 ㅎㅎㅎㅎㅎ...........

그리고........ 알고리즘 시험을 위해 IDE의 자동완성 기능을이용하지 않았다...

진짜 엄청나게 IDE가 개발 효율성을 높여준다는 생각이 든다.

 

public class 단어공부 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str = sc.next().toUpperCase();
		int strLen = str.length();
		
		boolean[] checkArr = new boolean[strLen];
		Map<String, Integer> hm = new HashMap<String, Integer>();
		int max =-1;
		for(int i=0;i<strLen;i++) {
			if(checkArr[i]) continue;
			
			checkArr[i]= true;
			String temp = str.charAt(i)+"";
			int count = 0;
			for(int j=i+1;j<strLen;j++) {
				if((!checkArr[j])&&(temp.equals(str.charAt(j)+""))) {
					count++;
					hm.put(temp, count);
					checkArr[j] = true;
				}
			}
			if(count==0) hm.put(temp,0);
			
			if(max<count) max =count;
		}
		int maxCount=0;
		String answer ="";
		for(String key: hm.keySet()) {
			if(maxCount>=2) {
				answer ="?";
				break;
			}
			
			if(hm.get(key)==max) {
				maxCount++;
				answer = key; 
			} 
		}
		System.out.println(answer);
	}
}

 응 이건아니자나..........

꼬아서 생각하지 말자!

어차피 A-Z까지 밖에없다... 배열 만들어서 count 갯수 넣어주면 됐어........

 

 

import java.util.*;

public class 단어공부 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str = sc.next().toUpperCase();
		int[]  numArr = new int[26];
		
		for(int i=0;i< str.length();i++) {
			int num =  str.charAt(i)-65;
			numArr[num]++;
		}
		
		int max = -1;
		int maxindex =-1;
		for(int i=0;i<numArr.length;i++) {
			int temp = numArr[i];
			if(temp>max) {
				max = temp;
				maxindex = i;
			}
		}
		int maxCount =0;
		for(int i=0;i<numArr.length;i++) {
			if(max==numArr[i]) maxCount++;
		}
		
		if(maxCount>=2) {
			System.out.println("?");
			return;
		} 
		char answer =  (char)(maxindex+65);
		if(maxCount==1) {
			System.out.println(answer);
			return;
		}
	}
}

이것도 뭔가 복잡해보이지만.........

음 그냥 마무리하고 퇴근할래.............

'Algorithm' 카테고리의 다른 글

크레인 인형뽑기 게임  (0) 2020.08.25
SQL 정리  (0) 2020.05.27
[프로그래머스] SQL문제풀기 & 정리  (0) 2020.05.25
[백준] 2667 단지번호붙이기  (0) 2020.04.20
[백준] DFS와 BFS  (0) 2020.04.06
Comments