解题思路
由于子串是连续的,问题等价于检查 是否能通过切割和重新排列子串形成 。只需验证 和 的字符频率是否相同,因为子串拼接不改变字符总数。使用数组统计 和 中各字符的出现次数,若频率一致则输出 “Yes”,否则输出 “No”。时间复杂度 。
#include <bits/stdc++.h>//I love cpeditor
using namespace std;
int b, e[114], f[114];
int main() {
cin >> b;
string c, d;
cin >> c >> d;
for (int i = 0; i < b; i++) {
e[c[i] - 'a']++;
f[d[i] - 'a']++;
}
bool g = true;
for (int i = 0; i < 26; i++) {
if (e[i] != f[i]) {
g = false;
break;
}
}
cout << (g ? "Yes" : "No");
return 0;
}