반응형
문제 링크 : https://www.acmicpc.net/problem/10808
10808번: 알파벳 개수
단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다.
www.acmicpc.net
접근방식
알파벳의 개수를 세기 위한 배열을 선언 후에 단어를 구성하는 알파벳을 하나씩 확인한 후에 알파벳의 대응되는 인덱스 값의 위치를 1씩 증가시킨다. 그렇게 하면 O(n) 시간이 걸린다.
시간 복잡도
O(n)
코드
#include <bits/stdc++.h>
using namespace std;
string arr;
int alphabet[26] = { 0, }; //알파벳 개수를 저장하기 위한 배열
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> arr;
//a~z까지 차례로 숫자0부터 25의 값을 지정
for (int i = 0; i < arr.size(); i++) {
alphabet[arr[i] - 97] += 1;
}
for (int i = 0; i < 26; i++) {
cout << alphabet[i] << ' ';
}
return 0;
}
다른 문제의 코드 : https://github.com/DaeeYong/Algorithm-Solution-
DaeeYong/Algorithm-Solution-
Solution for Algorithm Problem. Contribute to DaeeYong/Algorithm-Solution- development by creating an account on GitHub.
github.com
반응형
'알고리즘 > 백준 문제풀이' 카테고리의 다른 글
[C/C++]백준 2490번 - 윷놀이 (0) | 2021.10.20 |
---|---|
[C/C++]백준 2752번 - 세수정렬 (0) | 2021.10.18 |
[C/C++]백준 2630번 - 색종이 만들기 (0) | 2021.09.24 |
[C/C++]백준 1992번 - 쿼드트리 (0) | 2021.09.22 |
[C/C++]백준 2503번 - 숫자야구 (0) | 2021.09.01 |