백준 - 알파벳 개수

phoenixKim·2022년 5월 16일
0

백준 알고리즘

목록 보기
22/174

언제품?

: 220915 1:22 ~ 1:26

접근

단어 : baekjoon
에서 각 알파벳이 사용된 횟수를 구하는 것임.

int a, int b, int c,,,,~~ int z까지가 있고, 초기화는 모두 0임.

-> 배열로 나타내면 a는 0번째 인덱스로 나타낼 수 있음.

저 단어에서 하나씩 뺀 다음에 카운딩을 하자.
b -> arr[1]++;
a -> arr[0]++;
e -> arr[4]++;
~~ 이런식으로 하면 되지 않을까 싶음?

코드

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
#include <algorithm>
#include <vector>



int main()
{
	string s = "baekjoon";

	// 알파벳 26개라고 함.

	int arr[26]{0,};
	// a가 0번째 인덱스~
	
	for (auto iter : s)
	{
		int index = iter - 'a';
		arr[index]++;
	}

	for (int i = 0; i < 26; i++)
	{
		cout << arr[i] << " ";
	}
}

profile
🔥🔥🔥

0개의 댓글

관련 채용 정보