Maximum Palindromes After Operations - Problem

Imagine you have a collection of string words and magical powers to rearrange any characters between them! ๐ŸŽญ

You're given an array of strings words, and you can perform an unlimited number of character swaps. In each operation, you can:

  • Choose any two strings (they can be the same string)
  • Pick any character position in each string
  • Swap those two characters

Your goal is to determine the maximum number of palindromes you can create from these words after performing optimal swaps.

Example: If you have ["abc", "def"], you could swap characters to potentially create ["aba", "dfd"] - two palindromes!

The key insight is that since you can swap any characters between any words, what matters is the total count of each character across all words, not their initial positions.

Input & Output

example_1.py โ€” Basic Case
$ Input: words = ["abde", "cxyz", "abc"]
โ€บ Output: 2
๐Ÿ’ก Note: We have characters: a=2, b=2, c=2, d=1, e=1, x=1, y=1, z=1. This gives us 3 pairs and 5 singles. Sorting word lengths: [3,4,4]. We can form 2 palindromes by using pairs optimally: one 3-length palindrome (1 pair + 1 single) and one 4-length palindrome (2 pairs).
example_2.py โ€” All Same Length
$ Input: words = ["abc", "def", "ghi"]
โ€บ Output: 0
๐Ÿ’ก Note: All characters appear only once, giving us 0 pairs and 9 singles. Each word needs at least 1 pair to form a palindrome of length 3, but we have no pairs available. Result: 0 palindromes.
example_3.py โ€” Perfect Palindromes
$ Input: words = ["aab", "baa", "cc"]
โ€บ Output: 3
๐Ÿ’ก Note: We have a=4, b=2, c=2, giving us 4 pairs and 0 singles. Word lengths [3,3,2] all can be formed into palindromes: length 3 needs 1 pair + 1 single (we can use 2 pairs as substitute), length 3 needs 1 pair + 1 single, length 2 needs 1 pair. Total: 3 palindromes.

Constraints

  • 1 โ‰ค words.length โ‰ค 105
  • 1 โ‰ค words[i].length โ‰ค 100
  • words[i] consists of lowercase English letters only
  • Key insight: Since any character can be moved to any position in any word, only total character counts matter

Visualization

Tap to expand
๐Ÿช The Palindrome Bakery Challengeaร—4bร—2cร—2Available cookiesPairs: 4 | Singles: 0Jar 1Length: 2Needs: 1 pairโœ“ Can make!Jar 2Length: 3Needs: 1 pair + 1 singleโœ“ Can make!Jar 3Length: 4Needs: 2 pairsโœ— Not enough!Greedy Strategy: Start with Smallest Jars!1. Jar 1 (length 2): Use 1 pair โ†’ aa (palindrome โœ“)2. Jar 2 (length 3): Use 1 pair + convert 1 pair to singles โ†’ aba (palindrome โœ“)3. Jar 3 (length 4): Need 2 pairs but only 1 left โ†’ cannot make palindrome โœ—Result: 2 palindromes maximum!๐Ÿ’ก Key Insight: Greedy works because smaller jars are cheaper to fill!Why start small? A 2-length palindrome needs 1 pair, but 4-length needs 2 pairs.
Understanding the Visualization
1
Inventory Count
Count all your cookies by letter type across all jars
2
Resource Planning
Calculate how many 'pairs' and 'single' cookies you have
3
Smart Assignment
Start with the smallest jars - they need fewer cookies to form palindromes
4
Maximize Output
Continue until you run out of cookies or jars
Key Takeaway
๐ŸŽฏ Key Insight: Since characters can be freely redistributed, we only care about total character counts. The greedy strategy of filling shortest words first maximizes palindromes because they require fewer resources.
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 24
38.9K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen