Maximum Palindromes After Operations

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.

Example 1
Inputwords = ["abbb","ba","aa"]
Output3
By swapping words[0][0] and words[1][0], words becomes ["bbbb", "aa", "aa"], so all three strings are palindromes.
Example 2
Inputwords = ["abc","ab"]
Output2
After swaps, words can become ["aca", "bb"], so both strings are palindromes.

Constraints

  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 100
  • words[i] consists only of lowercase English letters.

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate