题面
khba is writing his girlfriend’s name. He has cubes, each with one lowercase Latin letter written on it. They are arranged in a row, forming a string . His girlfriend’s name is also a string , consisting of lowercase Latin letters.
To prove his love, he must check whether it is possible to rearrange the letters of string so that it becomes her name .
Input
The first line contains an integer () — the number of test cases.
The first line of each test case contains an integer ().
The second line of each test case contains two distinct strings and , each consisting of lowercase Latin letters.
Output
For each test case, output “YES” if the letters of can be arranged to form ; 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.
思路
我们只需要检查字符串 是否有可能排列出字符串 即可
但是需要注意的是:
- 对应字符都要出现
- 对应字符个数要满足重排 的需求,换言之,对应字符可以比需求字符多,但是不能少
代码
#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; }}