3035 - Maximum Palindromes After Operations (Medium)
Problem Link
https://leetcode.com/problems/maximum-palindromes-after-operations/
Problem Statement
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 (including zero):
- Choose integers
i
,j
,x
, andy
such that0 <= i, j < n
,0 <= x < words[i].length
,0 <= y < words[j].length
, and swap the characterswords[i][x]
andwords[j][y]
.
Return an integer denoting the maximum number of palindromeswords
can contain, after performing some operations.
Note: i
and j
may be equal during an operation.
Example 1:
Input: words = ["abbb","ba","aa"]
Output: 3
Explanation: 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 is 3.
Example 2:
Input: words = ["abc","ab"]
Output: 2
Explanation: 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 is 2.
Example 3:
Input: words = ["cd","ef","a"]
Output: 1
Explanation: 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 is 1.
Constraints:
words[i]
consists only of lowercase English letters.
Approach 1: Counting
The frequency of each character matters. We calculate and fill the character based on the word length. For example, if we got four a
, then we have make pairs. If we got five a
, we can still make pairs and we have an extra a
to be used for the word with odd length (e.g. b_b
-> b[a]b
).
- Time Complexity:
- Space Complexity:
- C++
class Solution {
public:
int maxPalindromesAfterOperations(vector<string>& words) {
int n = words.size(), ans = 0;
// hash map to store the frequency of characters
unordered_map<char, int> cnt;
// count the frequency for each character
for (auto word : words) for (auto x : word) cnt[x]++;
// we wanna know how many pairs `p` we can make
// and how many characters with odd frequency
int p = 0, odd = 0;
for (auto x : cnt) p += x.second / 2, odd += x.second % 2;
// we need to construct in a greedy fashion
// sort the input by size in ascending order
sort(words.begin(), words.end(), [](const string& s, const string& t) {
return s.size() < t.size();
});
// iterate each word in sorted `words`
for (auto word: words) {
// `l` is the size of the current word
int l = word.size();
// we need `l / 2` pairs to form the palindrome
if (l / 2 <= p) {
// if we have enough pairs `p`, then use them (update p)
// if this word has odd length, then we need an extra character (taken from `odd`)
// increase `ans` by 1 since this word is a palindrome.
p -= l / 2, odd -= l % 2, ans += 1;
// if we didn't have odd but we needed one, we borrow from `p`
if (odd < 0) odd += 2, p -= 1;
}
}
return ans;
}
};