题解 | 小牛的作业
小牛的作业
https://www.nowcoder.com/practice/2003093910a142509157fa2f94088a0d
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string s, t;
cin >> s >> t;
if (s.length() < t.length()) {
cout << -1 << endl;
return 0;
}
// 统计频率
vector<int> cnt_s(26, 0), cnt_t(26, 0);
// 只取 s 的前 t.length() 个字符
for (int i = 0; i < s.length(); ++i)
cnt_s[s[i] - 'a']++;
for (char c : t)
cnt_t[c - 'a']++;
int changes = 0;
for (int i = 0; i < 26; ++i) {
if (cnt_s[i] < cnt_t[i])
changes += cnt_t[i] - cnt_s[i];
}
changes+=s.size()-t.size();
cout << changes << endl;
return 0;
}
