pyassistant

Minimum adjacent swaps to make a string a palindrome

Given a string s consisting of lowercase letters, compute the minimum number of adjacent swaps required to rearrange s into a palindrome. If it is impossible to rearrange s into any palindrome, return -1. An adjacent swap exchanges two neighboring characters (positions i and i+1). You must count the minimal number of such swaps needed to obtain any palindrome arrangement of the characters. Constraints: s length up to a few thousand (your algorithm should be at most O(n^2) time and O(n) extra space).

Example:

Input:
('mamad',)
Output:
3

Make sure you return your solution, don't print!

AI

Bot

Trying to solve my challenge? Ask if you must, or press the purple button so I can analyze your code.