BOJ 10808. 알파벳 개수
BOJ 10808. 알파벳 개수
풀이
알파벳 a~z의 개수를 저장하는 26사이즈의 배열을 만들어 카운트한다.
a가 아스키코드 97
b가 아스키코드 98
c가 아스키코드 99
…
z가 아스키코드 122 이므로
char에서 97을 빼면 index를 얻을 수 있다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int[] alphabet = new int[26];
for (int i = 0; i < s.length(); i++) {
int idx = s.charAt(i) - 97;
alphabet[idx]++;
}
for (int i = 0; i < alphabet.length; i++) {
System.out.print(alphabet[i] + " ");
}
}
}