← 返回 ibm 的题目列表Maximum Palindromes After Cross-String Swaps
类型:qbank
Given an array of lowercase strings, any operation may swap one character from one string with one character from another string. Return the maximum number of strings that can be made palindromic.
Requirements
Input: integer n and string array arr of size n, containing lowercase letters.
Operation: any number of times, choose two different strings and swap one character from each.
Output: the maximum possible number of palindromic strings after performing operations.
String lengths are fixed; characters can effectively be redistributed globally through repeated swaps.
Examples
arr = ["pass", "sas", "asps", "df"]
Output: 3
Notes
Count global character frequencies across all strings.
A palindrome of length L needs floor(L / 2) character pairs, plus one single character if L is odd.
Sort or process target string lengths so pair capacity is spent efficiently.
Track available pairs and singles globally; the answer is how many string lengths can be satisfied under those resources.
Preparation
Convert global character counts into pairs = sum(freq // 2) and singles = sum(freq % 2), then greedily spend pairs by target string length.
Practise odd-length accounting: an odd palindrome needs one center, but unused pairs can be split into two singles if necessary after pair requirements are met.