387 字
2 分钟
Your-Name

题面#

khba is writing his girlfriend’s name. He has nn cubes, each with one lowercase Latin letter written on it. They are arranged in a row, forming a string ss. His girlfriend’s name is also a string tt, consisting of nn lowercase Latin letters.

To prove his love, he must check whether it is possible to rearrange the letters of string ss so that it becomes her name tt.

Input

The first line contains an integer qq (1q10001 \le q \le 1000) — the number of test cases.

The first line of each test case contains an integer nn (1n201 \le n \le 20).

The second line of each test case contains two distinct strings ss and tt, each consisting of nn lowercase Latin letters.

Output

For each test case, output “YES” if the letters of ss can be arranged to form tt; otherwise, output “NO”.

You can output the answer in any case (upper or lower). For example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as positive responses.

思路#

我们只需要检查字符串 ss 是否有可能排列出字符串 tt 即可

但是需要注意的是:

  • 对应字符都要出现
  • 对应字符个数要满足重排 tt 的需求,换言之,对应字符可以比需求字符多,但是不能少

代码#

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t --) {
int n;
string s, t;
cin >> n >> s >> t;
map<char, int> counts;
for (int i = 0; i < n; i ++) {
if (counts.find(s[i]) == counts.end()) {
counts[s[i]] = 1;
}
else if (counts.find(s[i]) != counts.end()) counts[s[i]] ++;
}
map<char, int> matches;
for (int i = 0; i < n; i ++) {
if (matches.find(t[i]) == matches.end()) {
matches[t[i]] = 1;
}
else if (matches.find(t[i]) != matches.end()) matches[t[i]] ++;
}
bool success = true;
for (int i = 0; i < n; i ++) {
if (matches.find(t[i]) == matches.end()) {
success = false;
break;
}
if (matches[t[i]] > counts[t[i]]) {
success = false;
break;
}
}
if (success) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
Your-Name
https://github.com/posts/your-name/
作者
FZSGBall
发布于
2026-05-28
许可协议
CC BY-NC-SA 4.0