콘솔창 & 윈도우창/코딩 테스트

Anagram문제

게임 개발 2024. 4. 11. 19:39

 

 

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <algorithm>

using namespace std;

int a[60], b[60];


int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	char str[101];

	int i;
	cin >> str;

	for (i = 0; str[i] != '\0'; i++)
	{
		if (str[i] >= 65 && str[i] <= 90)
			a[str[i] - 64]++;

		else
			a[str[i] - 70]++;
	}
	cin >> str;

	for (i = 0; str[i] != '\0'; i++)
	{
		if (str[i] >= 65 && str[i] <= 90)
			b[str[i] - 64]++;
		else
			b[str[i] - 70]++;
	}

	for (i = 1; i <= 52; i++)
	{
		if (a[i] != b[i])
		{
			cout << "NO\n";
			exit(0);
		}
	}

	cout << "YES\n";


	return 0;
}

 

 

1. 1~52까지

대문자부터 소문자의 개수를 담는 인덱스를 주어진 글자 수의 최대까지 미리 만들어준다.

 

2. 이후 문자열을 받아온 뒤 인덱스 별로 알파벳 대문자 소문자를 구분하여 그 개수를 체크해준다.

 

3. 체크된 알파벳 개수를 짝맞춰본뒤

만약 짝이 맞지 않다면 NO를 출력하고 프로그램을 종료하도록 한다.

 

4. 만약 짝이 맞지 않은 경우에 걸리지 않았다면 

YES를 출력하고 프로그램이 종료된다.