본문 바로가기

[백준] 1919 애너그램 만들기

@cayman0312025. 12. 21. 15:02

 

풀이

문제에서 주어진 조건대로 애너그램을 만들려면 주어진 영어단어에서 알파벳의 수가 같아야한다.

즉, 단어1에서 a~z까지의 각 문자가 몇번 등장하는지 세고 단어2에서 a~z까지의 각 문자가 몇번 등장하는지 세어서 

단어별 차이만큼의 문자를 삭제해서 두 단어가 같아질 수 있도록 해주면 된다.

 

#include <bits/stdc++.h>
using namespace std;

int arr[2][26];
string s1, s2;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int ans = 0;

    cin >> s1 >> s2;

    for(char c:s1) arr[0][c-'a']++;
    for(char c:s2) arr[1][c-'a']++;

    for (int i = 0; i < 26; i++) {
        ans += abs(arr[0][i] - arr[1][i]);
    }

    cout << ans;


    return 0;
}

 

 

'Memo > PS' 카테고리의 다른 글

[백준] 2941 크로아티아 알파벳  (0) 2025.12.31
[백준] 1406 에디터  (0) 2025.12.23
[백준] 11328 Strfry  (0) 2025.12.19
[백준] 10807 개수 세기  (0) 2025.12.18
[백준] 2444 별찍기 - 7  (0) 2025.12.13
cayman031
@cayman031 :: 그누로그

목차