class Solution {
public:
bool isPalindrome(const string& word) {
return equal(word.begin(), word.begin() + word.size() / 2, word.rbegin());
public:
int maxPalindromesAfterOperations(vector<string>& words) {
int ans = 0;
unordered_map<char, int> charFreq;
// Store frequency of characters in all words
for (const string& word : words) {
for (char c : word) {
charFreq[c]++;
vector<int> lengths;
// Store lengths of all words
for (const string& word : words) {
lengths.push_back(word.length());
// Sort lengths in non-decreasing order
sort(lengths.begin(), lengths.end());
// Iterate through each length
for (int len : lengths) {
int requiredChars = len / 2 * 2; // Number of characters needed to form palindromes of this
length
// Try to form palindromes of this length using available characters
for (auto& entry : charFreq) {
int availChars = entry.second / 2 * 2; // Number of available characters for this character
if (availChars >= requiredChars) {
ans++; // Increment answer if it's possible to form a palindrome of this length
charFreq[entry.first] -= requiredChars;
break;
} else {
requiredChars -= availChars;
charFreq[entry.first] -= availChars;
return ans;
};