0% found this document useful (0 votes)
3 views2 pages

LCC Cafle4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

LCC Cafle4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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;

};

You might also like