Count minimum swap to make string palindrome

13 Jul, 2022
Given a string s, the task is to find out the minimum no of adjacent swaps required to make string s palindrome. If it is not possible, then return -1.
Examples:
Input: aabcb Output: 3 Explanation: After 1st swap: abacb After 2nd swap: abcab After 3rd swap: abcba
Input: adbcdbad Output: -1 
ApproachThe following are detailed steps to solve this problem. 
Check whether it is possible to make a palindrome or not from the given string. As we know that if more than one character in a string occurs an odd number of times that string can’t be a palindrome. 
If palindrome is not possible then return -1.
Take two pointers left pointing to the 0th index and a right pointer pointing to the last index to the given string
Do the following until left pointer is less than right pointer:Fix the left pointer and move a copy of right pointer say r, to rightside to search element which is similar to character pointing by left pointer.If the left pointer is equal to r pointer, it means this is an odd occurring character that we have to move at the middle of the string. So swap character at r index to its next index (move character toward right side)Increment result by 1 for this swap operation.Otherwise,Swap the found character at r to rightside, until it reaches at right index and keeps incrementing the result for the swap operation.Increment left by 1 and decrement right by 1.
Fix the left pointer and move a copy of right pointer say r, to rightside to search element which is similar to character pointing by left pointer.
If the left pointer is equal to r pointer, it means this is an odd occurring character that we have to move at the middle of the string. So swap character at r index to its next index (move character toward right side)Increment result by 1 for this swap operation.
 So swap character at r index to its next index (move character toward right side)
Increment result by 1 for this swap operation.
Otherwise,Swap the found character at r to rightside, until it reaches at right index and keeps incrementing the result for the swap operation.Increment left by 1 and decrement right by 1.
Swap the found character at r to rightside, until it reaches at right index and keeps incrementing the result for the swap operation.
Increment left by 1 and decrement right by 1.
Return the result.
Below is the implementation of the above approach: 
C++
#include <bits/stdc++.h>using namespace std; int minSwap(string s){    unordered_map<char, int> unmp;    int odd = 0, left = 0, right = s.size() - 1, result = 0;     for (char c : s)        unmp++;     for (auto i : unmp)        if (i.second % 2 == 1)            odd++;     if (odd > 1)        return -1;     while (left < right) {        int l = left, r = right;        while (s[l] != s[r])            r--;        if (l == r) {             // when we found odd element            swap(s[r], s[r + 1]);            result++;            continue;        }        else {            // Normal element            while (r < right) {                swap(s[r], s[r + 1]);                result++;                r++;            }        }        left++, right--;    }    return result;} int main(){    string s = "aabcc";    cout << minSwap(s);    return 0;}
4
Time Complexity: O(n2) Auxiliary Space: O(1)
sapnasingh4991
Adarsh Awasthi
jayaprakash akula
monjurulhaque2011
akshaysingh98088
shivanisinghss2110
ukasp
jyoti369
argha111234
ritwikyadav
harendrakumar123
palindrome
Strings
Strings
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.