-
Notifications
You must be signed in to change notification settings - Fork 22
/
anagram
58 lines (49 loc) · 1.38 KB
/
anagram
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are anagram of each other or
not. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example,
“act” and “tac” are anagram of each other.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case consist of two strings in 'lowercase' only,
in a single line.
Output:
Print "YES" without quotes if the two strings are anagram else print "NO".
Constraints:
1 ≤ T ≤ 300
1 ≤ |s| ≤ 1016
Example:
Input:
2
geeksforgeeks forgeeksgeeks
allergy allergic
Output:
YES
NO
Explanation:
Testcase 1: Both the string have same characters with same frequency. So, both are anagrams.
Testcase 2: Characters in both the strings are not same, so they are not anagrams.
*/
#include <bits/stdc++.h>
using namespace std;
bool anagram(string a, string b){
int count[26]={0};
if(a.size()!= b.size()) return false;
for(int i=0; i<a.size(); i++){
count[a[i]-'a']++;
count[b[i]-'a']--;
}
for(int i=0; i<26; i++){
if(count[i]>0){
return false;
}
}
return true;
}
int main() {
int t; cin>>t; while(t--){
string a,b;
cin>>a>>b;
if(!anagram(a,b)) cout<<"NO\n";
else cout<<"YES\n";
}
return 0;
}