Maximum Palindromes After Operations - Problem

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, 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 the maximum number of palindromes words can contain, after performing some operations.

Note: i and j may be equal during an operation.

Input & Output

Example 1 — Basic Case
$ Input: words = ["abc","car","a"]
Output: 3
💡 Note: We have characters: a,b,c,c,a,r,a → {a:3, b:1, c:2, r:1}. Pairs=2, odds=3. Words sorted by length: [3,3,1]. All three words can be formed: first two length-3 words each need 1 pair + 1 odd character, and the length-1 word needs 1 odd character. Total: 3 palindromes.
Example 2 — All Same Length
$ Input: words = ["ab","ty","yt"]
Output: 2
💡 Note: Characters: a,b,t,y,y,t → {a:1, b:1, t:2, y:2}. Pairs=2, odds=2. Each word needs 1 pair. We can form 2 palindromes like "ty" and "yt", but "ab" cannot be formed into palindrome with remaining characters.
Example 3 — Single Character Words
$ Input: words = ["a","b","a"]
Output: 2
💡 Note: Characters: a,b,a → {a:2, b:1}. Pairs=1, odds=1. All words have length 1 (need 0 pairs each). We can form 2 palindromes using the 2 'a' characters, but 'b' forms the third palindrome too. Total: 3, but wait - we have 2 odds available, so answer is 2.

Constraints

  • 1 ≤ words.length ≤ 105
  • 1 ≤ words[i].length ≤ 105
  • sum(words[i].length) ≤ 2 × 105
  • words[i] consists of lowercase English letters only

Visualization

Tap to expand
Maximum Palindromes After Operations INPUT words array (n=3) Index 0: "abc" len=3 Index 1: "car" len=3 Index 2: "a" len=1 All Characters a, b, c, c, a, r, a Total: 7 characters Lengths needed: [3, 3, 1] Sorted: [1, 3, 3] ALGORITHM STEPS 1 Count Characters a:3, b:1, c:2, r:1 2 Count Pairs Pairs: a(1), c(1) = 2 pairs Singles: a(1), b(1), r(1) = 3 3 Sort by Length Process shortest first [1, 3, 3] (greedy) 4 Build Palindromes len=1: need 0 pairs + 1 center Use 1 single --> "a" [OK] len=3: need 1 pair + 1 center Use aa+c --> "aca" [OK] len=3: need 1 pair + 1 center Use cc+? --> only 1 single left Can build! --> "bcb" [OK] FINAL RESULT Palindromes formed: Word 2 (len=1): "a" Word 0 (len=3): "aca" Word 1 (len=3): "bcb" Output: 2 All 3 words can be made into palindromes! Key Insight: Characters can be freely swapped between ANY words, so treat all characters as a shared pool. For palindromes: each word needs (length/2) pairs of characters, plus 1 center if odd length. Greedy approach: Sort words by length and assign pairs to shorter words first to maximize count. TutorialsPoint - Maximum Palindromes After Operations | Greedy Character Assignment
Asked in
Google 15 Microsoft 12
12.5K Views
Medium Frequency
~25 min Avg. Time
234 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