Count minimum swap to make string palindrome | C ++ program to Count minimum swap to make string palindrome ; Function to Count minimum swap ; calculate length of string as n ; counter to count minimum swap ; A loop which run till mid of string ; Left pointer ; Right pointer ; A loop which run from right pointer towards left pointer ; if both char same then break the loop . If not , then we have to move right pointer to one position left ; If both pointers are at same position , it denotes that we don 't have sufficient characters  to make palindrome string ; Driver code ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSwap ( string s ) { int n = s . length ( ) ; int count = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { int left = i ; int right = n - left - 1 ; while ( left < right ) { if ( s [ left ] == s [ right ] ) { break ; } else { right -- ; } } if ( left == right ) { return -1 ; } for ( int j = right ; j < n - left - 1 ; j ++ ) { swap ( s [ j ] , s [ j + 1 ] ) ; count ++ ; } } return count ; } int main ( ) { string s = " geeksfgeeks " ; int ans1 = countSwap ( s ) ; reverse ( s . begin ( ) , s . end ( ) ) ; int ans2 = countSwap ( s ) ; cout << max ( ans1 , ans2 ) ; return 0 ; }