# Minimum number of adjacent swaps to reverse a String

Given a string s. The task is to minimize the number of adjacent swaps required to reverse the string. Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.Input: s = “abc”Output: 3Explanation: Follow the operations below to solve the given problem.swap(1, 2)-> “bac”swap(2, 3)-> “bca”swap(1, 2)-> “cba”Input: s = “ba”Output: 1Approach: This problem can be solved by comparing the given string with its reverse and counting the number of swaps required to form the reversed string. Follow the steps below to solve the problem:Initialize a string s2 as the copy of the original string s.reverse string s2Initialize result = 0, to store the number of adjacent swaps required to reverse the string.Iterate using two pointers i and j for both the strings and find each occurrence of s in s2 through two pointers i and jEach time set result = result + j – i.Return result as the final answer.Below is the implementation of the above approach:C++#include using namespace std;int min_swaps(string s){ string s2 = s; reverse(s2.begin(), s2.end()); int i = 0, j = 0; int result = 0; int n = s.length(); while (i < n) { j = i; while (s[j] != s2[i]) { j += 1; } while (i < j) { char temp = s[j]; s[j] = s[j - 1]; s[j - 1] = temp; j -= 1; result += 1; } i += 1; } return result;}int main(){ string s = "abc"; cout