LeetCode 3035. Maximum Palindromes After Operations
原题链接
中等
作者:
_YH_YH_
,
2024-02-20 05:15:44
,
所有人可见
,
阅读 38
class Solution {
public:
int maxPalindromesAfterOperations(vector<string>& words) {
int cnt[26] = {0};
vector<int> f;
for (auto& s : words) {
f.push_back(s.size());
for (char c : s)
cnt[c - 'a']++;
}
int pairs = 0;
for (int i = 0; i < 26; ++i) pairs += cnt[i] / 2;
sort(f.begin(), f.end());
int res = 0;
for (int x : f) {
int need = x / 2;
if (need <= pairs) {
pairs -= need;
res++;
}
}
return res;
}
};