Algorithm/문자열 처리
Baekjoon 1919 애너그램 만들기
GirlFriend_Yerin
2019. 8. 21. 13:25
Link https://www.acmicpc.net/problem/1919
소스결과 1988 KB / 0 ms
출처 Baekjoon
언어 C++ 17
분류 문자열처리
설명
두 문장이 애너그램이 되기 위해서 지워야 하는 문자의 개수를 출력하자
알고리즘
1. 첫번째 문자열의 각 문자 개수를 배열에 저장한다.
2. 두번째 문자열의 각 문자 개수를 배열에 저장한다.
3. a ~ z 까지 큰 수 - 작은 수를 합해 출력한다.
소스코드
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
int alphaTop[26] = {}, alphaBot[26] = {}, res = 0;
string top, bot;
cin >> top >> bot;
for (int i = 0; i < top.size(); i++)
alphaTop[top[i] - 'a']++;
for (int i = 0; i < bot.size(); i++)
alphaBot[bot[i] - 'a']++;
for (int i = 0; i < 26; i++)
res += abs(alphaTop[i] - alphaBot[i]);
cout << res;
return 0;
}