You are given a 0-indexed string array words having length n and containing 0-indexed strings.
You are allowed to perform the following operation any number of times (includingzero):
Choose integers i, j, x, and y such that 0 <= i, j < n, 0 <= x < words[i].length, 0 <= y < words[j].length, and swap the characters words[i][x] and words[j][y].
Return _an integer denoting themaximum number of palindromes _wordscan contain, after performing some operations.
Input: words =["abbb","ba","aa"]Output: 3Explanation: In this example, one way to get the maximum number of palindromes is:Choose i =0, j =1, x =0, y =0, so we swap words[0][0] and words[1][0]. words becomes ["bbbb","aa","aa"].All strings in words are now palindromes.Hence, the maximum number of palindromes achievable is3.
Input: words =["abc","ab"]Output: 2Explanation: In this example, one way to get the maximum number of palindromes is:Choose i =0, j =1, x =1, y =0, so we swap words[0][1] and words[1][0]. words becomes ["aac","bb"].Choose i =0, j =0, x =1, y =2, so we swap words[0][1] and words[0][2]. words becomes ["aca","bb"].Both strings are now palindromes.Hence, the maximum number of palindromes achievable is2.
Input: words =["cd","ef","a"]Output: 1Explanation: In this example, there is no need to perform any operation.There is one palindrome in words "a".It can be shown that it is not possible to get more than one palindrome after any number of operations.Hence, the answer is1.
Since we can swap any characters between any words, the total multiset of all characters is fixed. To maximize the number of palindromes, we want to distribute the characters so that as many words as possible can be rearranged into palindromes. A word can be rearranged into a palindrome if at most one character has an odd count. We can greedily pair up characters and assign them to words, giving each word as many pairs as possible, and then assign odd characters as centers if needed.
classSolution {
public:int maxPalindromesAfterOperations(vector<string>& words) {
vector<int> lens;
vector<int> freq(26, 0);
for (auto& w : words) {
lens.push_back(w.size());
for (char c : w) freq[c -'a']++;
}
sort(lens.begin(), lens.end());
int pairs =0, odds =0;
for (int f : freq) {
pairs += f /2;
odds += f %2;
}
int ans =0;
for (int len : lens) {
int need = len /2;
if (pairs < need) break;
pairs -= need;
if (len %2) {
if (odds ==0) {
if (pairs ==0) break;
pairs--;
} else {
odds--;
}
}
ans++;
}
return ans;
}
};
classSolution {
funmaxPalindromesAfterOperations(words: Array<String>): Int {
val lens = words.map { it.length }.sorted()
val freq = IntArray(26)
for (w in words) for (c in w) freq[c - 'a']++var pairs = freq.sumOf { it / 2 }
var odds = freq.sumOf { it % 2 }
var ans = 0for (len in lens) {
val need = len / 2if (pairs < need) break pairs -= need
if (len % 2==1) {
if (odds ==0) {
if (pairs ==0) break pairs-- } else {
odds-- }
}
ans++ }
return ans
}
}
classSolution:
defmaxPalindromesAfterOperations(self, words: list[str]) -> int:
lens = sorted(len(w) for w in words)
from collections import Counter
freq = Counter()
for w in words:
freq.update(w)
pairs = sum(v //2for v in freq.values())
odds = sum(v %2for v in freq.values())
ans =0for l in lens:
need = l //2if pairs < need:
break pairs -= need
if l %2:
if odds ==0:
if pairs ==0:
break pairs -=1else:
odds -=1 ans +=1return ans
impl Solution {
pubfnmax_palindromes_after_operations(words: Vec<String>) -> i32 {
letmut lens: Vec<usize>= words.iter().map(|w| w.len()).collect();
lens.sort();
letmut freq = [0; 26];
for w in&words {
for c in w.chars() {
freq[c asusize-'a'asusize] +=1;
}
}
letmut pairs = freq.iter().map(|&f| f /2).sum::<usize>();
letmut odds = freq.iter().map(|&f| f %2).sum::<usize>();
letmut ans =0;
for&len in&lens {
let need = len /2;
if pairs < need { break; }
pairs -= need;
if len %2==1 {
if odds ==0 {
if pairs ==0 { break; }
pairs -=1;
} else {
odds -=1;
}
}
ans +=1;
}
ans asi32 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
maxPalindromesAfterOperations(words: string[]):number {
constlens=words.map(w=>w.length).sort((a, b) =>a-b);
constfreq=new Array(26).fill(0);
for (constwofwords) for (constcofw) freq[c.charCodeAt(0) -97]++;
letpairs=freq.reduce((a, b) =>a+ Math.floor(b/2), 0);
letodds=freq.reduce((a, b) =>a+ (b%2), 0);
letans=0;
for (constlenoflens) {
constneed= Math.floor(len/2);
if (pairs<need) break;
pairs-=need;
if (len%2===1) {
if (odds===0) {
if