필수개념/algorithm

백준 1157 - 단어공부

미침 2024. 9. 15. 23:41

본격적으로 알고리즘 공부를 한 번 해보기 위해서

워밍업을 하기 시작했다

 

백준 1157 - 단어공부

 

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

단, 대문자와 소문자를 구분하지 않는다.

 

A) 오랜만에 주어진 조건에 대해서 즉흥적으로 풀어보려고하니

뚝딱뚝딱...

public static void main(String[] args) {
        /*알파벳 개수, 26, 0으로 자동 초기화*/
        Scanner sc = new Scanner(System.in);
        String word = sc.next();

        long startTime = System.nanoTime();// 측정 시작 시간
        algorithm(word);
        long endTime = System.nanoTime(); // 측정 종료 시간
        long elapsedTime = endTime - startTime;
        double elapsedTimeInMillis = elapsedTime / 1_000_000.0;
        System.out.println("Elapsed Time in nanoseconds: " + elapsedTime);
        System.out.println("Elapsed Time in milliseconds: " + elapsedTimeInMillis);
}

/*처리부*/
public static void algorithm(String word) {
        int[] wordCntMax = new int[26];
        int maxIndex = -1, maxIndexWord = -1;
        boolean same = false;
        String upperWord = word.toUpperCase();
        for (int i = 0; i < upperWord.length(); i++) {
            char key = upperWord.charAt(i);
            char index = (char) (key - 'A');
            wordCntMax[index] = wordCntMax[index] + 1;
 
            if (wordCntMax[index] >= maxIndex) {
                if (wordCntMax[index] == maxIndex) {
                    same = true;
                } else {
                    same = false;
                }
                maxIndex = wordCntMax[index];
                maxIndexWord = index;
            }
        }
        char answer = same ? '?' : (char) (maxIndexWord + 'A');
        System.out.println(answer);
    }

 

AA) 아래는 다른 내용을 참고하여 한 번 작성해보았다

계속 풀어보다보면 잘 정리해서 작성할 수 있는 날이 오겠지...

public static void main(String[] args) {
        /*알파벳 개수, 26, 0으로 자동 초기화*/
        Scanner sc = new Scanner(System.in);
        String word = sc.next();

        long startTime2 = System.nanoTime();// 측정 시작 시간
        word = word.toUpperCase();
        int[] count = getCountAlphabet(word);
        int maxCount = -1;
        char maxAlphabetIndex = '?';
        for(int i = 0; i < count.length; i++){
            if(count[i] > maxCount){
                maxCount = count[i];
                maxAlphabetIndex = (char)('A'+i);
            }else if(count[i] == maxCount){
                maxAlphabetIndex = '?';
            }
        }
        System.out.println(maxAlphabetIndex);
        long endTime2 = System.nanoTime(); // 측정 종료 시간
        long elapsedTime2 = endTime2 - startTime2;
        double elapsedTimeInMillis2 = elapsedTime2 / 1_000_000.0;
        System.out.println("Elapsed Time in nanoseconds2: " + elapsedTime2);
        System.out.println("Elapsed Time in milliseconds2: " + elapsedTimeInMillis2);

    }
    
    public static int[] getCountAlphabet(String word) {
        int[] wordCntMax = new int[26];
        word = word.toUpperCase();
   
        for (int i = 0; i < word.length(); i++) {
            char key = word.charAt(i);
            wordCntMax[key - 'A']++;
        }
        return wordCntMax;
    }

 

오랜만에 문제를 풀어보려니 컴파일에러도 많이나고 답을 출력하지 않아 틀리고 그랬지만

워밍업 치고는 즐거운 시간이었다 

'필수개념 > algorithm' 카테고리의 다른 글

백준 13223 - 소금폭탄  (0) 2024.10.27
백준 1543 - 문서 검색  (0) 2024.10.20