Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
You are allowed to rearrange all letters of , so the current order of is probably less important than it looks.
Think about what rearranging preserves. It can move letters around, but it cannot create a missing letter or delete an extra one.
So the question becomes: do and contain the exact same letters with the exact same counts?
One easy way to compare letter counts is to sort both strings. If two strings have the same multiset of characters, their sorted versions are identical.
For each test case, sort and , then print YES if they are equal and NO otherwise.
The whole problem is asking whether is an anagram of .
That is it. No hidden romance mechanic, no cursed swap restriction, no anime lore check. If we can rearrange the letters of however we want, then the only thing that matters is the multiset of characters.
Key Observation
A rearrangement can change positions, but it cannot change letter counts.
For example, from humitsa, we can rearrange letters into mitsuha because both strings contain:
humitsaSame letters, same counts, different order. Totally fine.
But aakima cannot become makima, because:
aakima has three as and no m? Actually it has one m, one k, one i, and three as.makima has two ms, one a, one k, one i, one a total two as.The counts do not match, so no amount of swapping can fix it. Rearranging is powerful, but it is not alchemy.
How To Check This
There are two standard ways:
Since , sorting is absurdly cheap. We can just do:
YESNOWhy does sorting work?
Sorting puts equal letters into a canonical order. Any two strings with the same letters and counts will become exactly the same sorted string.
For example:
misaka sorted becomes aaikms
mikasa sorted also becomes aaikms
So the answer is YES.
Complexity
For each test case, sorting two strings of length costs:
With and , this is basically free. The memory usage is aside from the input strings.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int q;
cin >> q;
while (q--) {
int n;
string s, t;
cin >> n >> s >> t;
sort(s.begin(), s.end());
sort(t.begin(), t.end());
cout << (s == t ? "YES" : "NO") << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int q;
cin >> q;
while (q--) {
int n;
string s, t;
cin >> n >> s >> t;
sort(s.begin(), s.end());
sort(t.begin(), t.end());
cout << (s == t ? "YES" : "NO") << '\n';
}
return 0;
}