-
Notifications
You must be signed in to change notification settings - Fork 212
/
Anagram.cpp
62 lines (50 loc) · 1006 Bytes
/
Anagram.cpp
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
59
60
61
62
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
/* Function to check if two strings are anagram
* c, d: input string
*/
bool isAnagram(string c, string d){
int i=0;
int arrA[26];
int arra[26];
if(c.length()==d.length()){
for(i=0;i<26;i++){
arrA[i]=0;
arra[i]=0;
}
for(i=0;i<c.length();i++){
if(c[i]>=97)
++arra[c[i]-97];
else
++arrA[c[i]-65];
}
// Your code here
for(i=0;i<d.length();i++){
if(d[i]>=97)
--arra[d[i]-97];
else
--arrA[d[i]-65];
}
for(i=0;i<26;i++){
if(arra[i]!=0||arrA[i]!=0)
return false;
}
return true;
}
else
return false;
}
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while(t--){
string c, d;
cin >> c >> d;
if(isAnagram(c, d)) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
// } Driver Code Ends