💡 Codeing Test/백준

백준 10808번) 알파벳 개수 (JAVA)

밈98 2023. 3. 7. 23:00

바킹독님의 문제집을 풀면서 종종 백준 풀이를 할 예정이다!

https://github.com/encrypted-def/basic-algo-lecture/blob/master/workbook.md

 

GitHub - encrypted-def/basic-algo-lecture: 바킹독의 실전 알고리즘 강의 자료

바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.

github.com

 

문제

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

 

10808번: 알파벳 개수

단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다.

www.acmicpc.net

나의 풀이

알파벳 개수는 총 26개

import java.util.Scanner;
import java.io.IOException;


public class Main {
    public static void main (String[] args) throws IOException{
        Scanner sc = new Scanner(System.in);

        String str = sc.nextLine();
        char[] charArray = str.toCharArray();

        int[] alp = new int[26];
        for(int i=0;i<charArray.length;i++){
            alp[charArray[i]-97]+=1;
        }
        for(int i=0; i<alp.length;i++){
            System.out.print(alp[i]+" ");
        }

        sc.close();
    }
}

아스키 코드를 알면 풀 수 있다

숫자 0 = 48, 대문자 A = 65, 소문자 a = 97