Magicsheet logo

Maximum Palindromes After Operations

Medium
48%
Updated 6/1/2025

Maximum Palindromes After Operations

What is this problem about?

The Maximum Palindromes After Operations coding problem gives you an array of strings words. You are allowed to perform operations: choose two characters from any two strings (or from the same string), swap them, and then rearrange all characters into words. The goal is to maximize the total number of palindromic strings you can form from the characters of the original words array.

Why is this asked in interviews?

Squarepoint Capital, Microsoft, MathWorks, Grammarly, Expedia, and Bloomberg use this problem to test a candidate's understanding of string properties (palindromes), frequency counting, and greedy allocation. The key insight is that character availability is global, and the specific distribution across initial strings doesn't matter beyond their total counts.

Algorithmic pattern used

Frequency Counting + Greedy Allocation: The crucial observation is that character swaps allow you to move any character to any position in any string. This means the specific characters in words[i] are irrelevant; only the global counts of each character matter. To make a string a palindrome, at most one character can have an odd frequency count (for odd-length palindromes). For even-length palindromes, all characters must have even frequency counts.

Algorithm:

  1. Global Character Frequencies: Count the total occurrences of each character ('a' through 'z') across all strings in the words array.

  2. Count Pairs and Odd Characters: From the global frequencies:

    • total_character_pairs = 0: This is the sum of floor(frequency[char] / 2) for all characters. This represents the total number of character pairs we have available (e.g., 'a' appears 5 times, contributes 2 pairs).
    • total_odd_single_chars = 0: This is the sum of frequency[char] % 2 for all characters. This represents the total number of characters that have an odd count (e.g., 'a' appears 5 times, contributes 1 odd single char).
  3. Sort Word Lengths: Sort the lengths of the words in words in ascending order. We will try to form palindromes from the shortest words first, as this leaves more characters for longer words. This is a greedy choice because shorter words require fewer characters and are easier to satisfy.

  4. Greedy Palindrome Formation: Initialize palindromes_formed = 0. Iterate through the sorted word lengths: For each current_word_length: a. pairs_needed = current_word_length // 2. b. odd_slot_needed = current_word_length % 2. (1 if odd length, 0 if even length)

    c. Try to satisfy with available pairs: If total_character_pairs >= pairs_needed: total_character_pairs -= pairs_needed. // Use up required pairs If odd_slot_needed == 1: // If this is an odd-length word, it needs one single odd character If total_odd_single_chars > 0: // We have an existing odd character total_odd_single_chars--. Else: // No existing odd characters, so we must break a pair to get one. total_character_pairs--. // One character from a pair is used as the odd middle, the other is effectively lost for forming another pair. palindromes_formed++. Else: // Not enough pairs for this word. Since we sorted by length, we can't form any more palindromes. Break.

  5. Return palindromes_formed.

Example explanation

words = ["aabb", "abc"], N = 2.

  1. Global Frequencies: 'a': 3, 'b': 3, 'c': 1.
  2. Counts:
    • total_character_pairs: (3//2) for 'a' + (3//2) for 'b' + (1//2) for 'c' = 1 + 1 + 0 = 2.
    • total_odd_single_chars: (3%2) for 'a' + (3%2) for 'b' + (1%2) for 'c' = 1 + 1 + 1 = 3.
  3. Word lengths sorted: [3, 4] (from "abc", "aabb").
  4. Greedy formation:
    • Current word length = 3: pairs_needed = 1, odd_slot_needed = 1.
      • total_character_pairs (2) >= 1. Yes. total_character_pairs becomes 2 - 1 = 1.
      • odd_slot_needed = 1. total_odd_single_chars (3) > 0. Yes. total_odd_single_chars becomes 3 - 1 = 2.
      • palindromes_formed = 1.
    • Current word length = 4: pairs_needed = 2, odd_slot_needed = 0.
      • total_character_pairs (1) >= 2. No. Cannot form this palindrome. Break.

Result: palindromes_formed = 1. The example shows that if you have chars {'a':3, 'b':3, 'c':1}, you can make "aba" (len 3) and "bb" (len 2). This gives 2 palindromes. The problem is about forming new words, not restricted to the original lengths. Let's consider the number of total characters: 3+3+1 = 7. If you want to form two palindromes:

  • If length 3, 4: "aba" (uses 2a, 1b). Rem: a:1, b:2, c:1. From this, "bb" (len 2) is formed. Total 2.
  • My interpretation of word lengths was literal. "into words" meant into strings of those specific lengths.

Re-reading the problem carefully: "rearrange all characters into words." This strongly implies that the given words array defines the target lengths of the palindromes we want to form. My initial parsing of the problem statement for the example was probably correct. The solution's output for the example should be 1. It is possible the problem statement in the prompt is a simplified version of a more complex problem, or the example implies specific words to form. Let's assume the current greedy is correct given the current interpretation.

Common mistakes candidates make

  • Ignoring global character counts: Focusing on characters within individual words[i] before operations. The swaps make character availability global.
  • Incorrectly handling odd/even length palindromes: The rule of at most one odd-frequency character is key.
  • Sub-optimal greedy choice: Not sorting word lengths or prioritizing using pairs/odd characters efficiently.

Interview preparation tip

For the Array Hash Table Counting Sorting String Greedy interview pattern, this problem is a good test of consolidating information (character frequencies), then applying a greedy strategy based on optimal allocation (shortest words first) and palindrome formation rules.

Similar Questions