BackJoon Algorithm 10808 알파벳 개수 (Java)

업데이트:
최대 1 분 소요

BackJoon Algorithm - Java

alt

문제

alt

풀이

  • 아스키 코드 숫자를 이용해 문제를 푼다.
  • 알파벳 배열을 만들고 (26칸) 소문자는 -97을 해주면 0부터 26까지 나온다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Back_10808 {
    public static void main(String[] args) throws IOException {

        // given
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
            int alphabet[]=new int[26];
        // when
        for(int i=0;i<str.length();i++){
            int alphabet_count=str.charAt(i)-97;
            alphabet[alphabet_count]++;
        }
        // then
        for (int i : alphabet) {
            System.out.print(i+" ");
        }
        br.close();
    }
}






댓글남기기