Ctrl K

Palindromes From Shared Letters

Medium
A board shows the strings tiles, made of letter magnets. Any two magnets may swap places, whether they sit in the same string or in different ones, and you may make as many swaps as you like. The length of each string stays the same. Return the largest number of strings that can be palindromes (reading the same forwards and backwards) at the same time after the swaps.
Example 1
tiles=['xyx', 'yy', 'xz'] → 2

The pool holds x three times, y three times and z once: that is only two letter pairs (one of x, one of y). Each of the three strings needs one pair, so at most two of them can become palindromes, for example "xzx" and "yy".

Example 2
tiles=['ba', 'ab', 'c'] → 3

Swap the a of the first string with the b of the second to get "bb" and "aa"; "c" is a palindrome already. All three strings are palindromes.

Example 3
tiles=['pq', 'rs', 'tu'] → 0
Constraints
  • 1 ≤ len(tiles) ≤ 1000
  • 1 ≤ len(tiles[i]) ≤ 100
  • tiles[i] consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsJobs With a Cooldown MediumMost Searched Words MediumNo Twin Letters Side by Side MediumSpread Out the Labels MediumPick Within Tag Limits Medium
loading Python…
Input
tiles=['xyx', 'yy', 'xz']
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 98 tests on the server, hidden ones included.