코딩가딩가

[JAVA]BOJ 1157

Noooodle 2024. 1. 2. 14:28

문제

1919번 처럼 String에 대해 알파벳 개수를 세는 배열을 만들고, 그 배열에서 가장 큰 값을 가진 알파벳을 출력한다. 가장 많이 사용된 알파벳이 여러개 존재할 경우에는 flag를 이용해 ?를 출력하도록 만들었다.

import java.util.Scanner;

public class BOJ1157 {
    public static int[] getAlphabetCount(String str){// str에 영어 대소문자 개수 세는 함수
        int[] count = new int[26];
        char ch;

        for(int i=0;i<str.length();i++) {
            ch = str.charAt(i);
            if(ch>='a'&&ch<='z') // 소문자일때
                count[ch - 'a']++;
            else if(ch>='A'&&ch<='Z') // 대문자일때
                count[ch - 'A']++;
        }

        return count;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int[] count = getAlphabetCount(str);
        int max = 0, max_idx = -1, i, flag = 0;

        for(i=0;i<26;i++){
            if(count[i] > max){
                max = count[i];
                max_idx = i;
                flag = 0;
            }
            else if(count[i] == max)
                flag = 1;
        }

        if(flag == 1)
            System.out.println("?");
        else
            System.out.println((char)(max_idx+'A'));
    }
}

max는 몇 번 나왔는지 횟수, max_idx는 max의 인덱스를 의미한다. 출력값이 횟수가 아니라 많이 나온 알파벳이라서 max_idx + 'A' 라고 출력해줘야 한다. flag를 이용했지만 쓰지 않고도 출력할 수 있다.


또 다른 방법으로는 반복문으로 A부터 Z까지 str에 몇개 있는지 count를 세는 방식이 있다. 알파벳 배열을 만들지 않는다.

import java.util.Scanner;

public class BOJ1157 {
    // 알파벳 한 글자를 가져와서 str과 비교하는 방법
    public static int getAlphabetCount(String str, char alp){
        int count = 0;

        for(int i=0;i<str.length();i++)
            if(alp == str.charAt(i))
                count++;

        return count;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next().toUpperCase(); // 대문자로만 입력 받기
        int max = -1, flag = 0;
        char maxAlp = '?';

        for(char alp = 'A';alp<='Z';alp++){
            int count = getAlphabetCount(str, alp);
            if(count > max){
                max = count;
                maxAlp = alp;
                flag = 0;
            }
            else if(count == max) {
                flag = 1;
            }
        }

        if(flag == 1)
            System.out.println("?");
        else
            System.out.println((char)(maxAlp));
    }
}

위의 코드는 flag 사용

import java.util.Scanner;

public class BOJ1157 {
    // 알파벳 한 글자를 가져와서 str과 비교하는 방법
    public static int getAlphabetCount(String str, char alp){
        int count = 0;

        for(int i=0;i<str.length();i++)
            if(alp == str.charAt(i))
                count++;

        return count;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next().toUpperCase(); // 대문자로만 입력 받기
        int max = -1;
        char maxAlp = '?';

        for(char alp = 'A';alp<='Z';alp++){
            int count = getAlphabetCount(str, alp);
            if(count > max){
                max = count;
                maxAlp = alp;
            }
            else if(count == max)
                maxAlp = '?';
        }

        System.out.println((char)(maxAlp));
    }
}

위의 코드는 flag 미사용

'코딩가딩가' 카테고리의 다른 글

[JAVA]BOJ 13223  (0) 2024.01.10
[JAVA]BOJ 1543  (0) 2024.01.04
[JAVA]BOJ 1919  (0) 2024.01.01
[JAVA]BOJ 2744  (1) 2023.12.31
[JAVA]String  (0) 2023.12.28