-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path004_Anagram.cpp
52 lines (35 loc) · 871 Bytes
/
004_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
class Solution
{
public:
//Function is to check whether two strings are anagram of each other or not.
bool isAnagram(string str1, string str2){
// Get lengths of both strings
int n1 = str1.length();
int n2 = str2.length();
// If length of both strings is not
// same, then they cannot be anagram
if (n1 != n2)
return false;
// Sort both the strings
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
// Compare sorted strings
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;
return true;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while(t--){
string c, d;
cin >> c >> d;
Solution obj;
if(obj.isAnagram(c, d)) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
// } Driver Code Ends