알고리즘 문제풀이

[백준 - 10808 Java] 알파벳 개수

TutleKing 2023. 3. 14. 21:10

단순한 문자 개수 세기 문제였다.

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();

        char[] chars = s.toCharArray();

        int[] result = new int[26];
        for (char aChar : chars) {
            result[(aChar - 'a')]++;
        }

        for (int i : result) {
            System.out.print(i + " ");
        }
    }
}

Character 는 int로도 따로 캐스팅 필요 없이 치환이 가능하다. ('a' -> 97) 고로 인덱스로 사용이 가능해진다. 

 

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

반응형