Problem is given two strings, 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 ≤ 30 1 ≤ |s| ≤ 10^16
Example: Input: 1 allergy allergic
Output: NO
My approach is to first check the length of strings, if they are not equal then simply print out NO and if they are equal then sort the strings and then compare element wise.
I am getting correct output on some simple inputs but not on complex ones as my code is not able to pass all the test cases.I am not able to figure out my mistake.
#include <iostream>
using namespace std;
#include<string>
#include<algorithm>
int main() {
int n, t=0;
cin>>n;
for(int i=0;i<n;i++)
{
string a,b;
cin>>a>>b;
cout<<"\n";
if(a.length()!=b.length())
{
cout<<"NO";
}
else
{
sort(a.begin(),a.end());
sort(b.begin(),b.end());
for(int i=0;i<a.length();i++)
{
if(a[i]!=b[i])
{
cout<<"NO";
break;
}
t++;
}
if(t==a.length())
{
cout<<"YES";
}
}
}
return 0;
}