본문 바로가기
SWE/코테

[성실코딩 14일차] 백준 #6996 애너그램 X

by S나라라2 2018. 10. 30.
반응형
#include <iostream>
#include <string>

using namespace std;

int main() {

	int tc;

	// 입력
	cin >> tc;
	for (int i = 0; i < tc; i++) {
		bool flag = true;
		char visited1[26] = { 0, };
		char visited2[26] = { 0, };

		string str1, str2;
		cin >> str1 >> str2;
		
		if (str1.length() != str2.length()) { // 둘의 길이가 다르다면
			flag = false;
		}
		else {
			for (int j = 0; j < str1.length(); j++) { // 길이 같음
				visited1[str1[j] - 'a'] ++; // str1방문
				visited2[str2[j] - 'a'] ++; // str2방문
			}

			for (int k = 0; k < 26; k++) {
				if (visited1[k] != visited2[k]) {
					flag = false;
					break;
				}
			}
		}

		if (flag==true) {
			cout << str1 << " & " << str2 << " are anagrams." << endl;
		}
		else {
			cout << str1 << " & " << str2 << " are NOT anagrams." << endl;
		}
	}
	return 0;
}
반응형