
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum Number of Adjacent Swaps to Reverse a String
A string str is given and we can swap only adjacent characters to make the string reverse. We have to find the number of minimum moves required to make the string reverse just by swapping the adjacent characters. We will implement two approaches to find the required solution with the explanation and the implementation of the code.
Sample Examples
Input1: string str1 = "shkej"
Output: 10
Explanation
First, we will take the last character to the first position which will take 4 swappings, and then the string will be "jshke". Then we will move ?e' to the second position, this will take 3 swappings. Similarly, for the ?k' we need two swappings and for ?h' just 1 swapping, finally answer is 10.
Input2: string str1 = "abace"
Output: 6
Explanation
First, we will swap the character at the 2nd index to take it to the last index will cost 2 swaps and the string will be "abcea". Then we will swap ?b' to ?e' will cost 2 swaps and the string will be "aceba". Then 2 more swaps to get the final reverse string with 6 swaps.
Naive Approach
We have seen the examples above, and now let us move to the steps that are required to implement the proper code.
First, we will create a function that will take the given string as the parameter and will return the minimum number of steps required as the return value.
In the function, we will create a copy of the string and then will reverse it to compare it with the original string.
We will create three variables, the first two are for the traversal over the strings and the last one is for storing the number of steps required.
By using the while loop we will traverse over the given string and will continue to skip the number of iterations where the current index value is the same as the reversed string.
Then we will use the while loop to swap the adjacent characters until ?j' reaches ?i' and will increase the count on each swap.
At last we will return the value of the count and will print that in the main function.
Example
#include <bits/stdc++.h> using namespace std; // function to get the minimum number of swaps int minSwaps(string str){ string temp = str; reverse(temp.begin(), temp.end()); // reversing the string int i = 0, j = 0; int ans = 0; int len = str.size(); while (i <len) { j = i; // find the character that is equal to the current element while (str[j] != temp[i]) { j++; } // iterating util the current i is equal to j while (i < j) { char tempc = str[j]; str[j] = str[j - 1]; str[j - 1] = tempc; j--; ans++; } i++; } return ans; } int main(){ string str = "efabc"; // given string // calling the function to find the minimum number of steps required cout<<"The minimum number of steps required to reverse the given string by swapping the adjacent characters is "<<minSwaps(str)<<endl; return 0; }
Output
The minimum number of steps required to reverse the given string by swapping the adjacent characters is 10
Time and Space Complexity
The time complexity of the above code is O(N^2), where N is the length of the given string. We are using the nested while loop gives us the factor of N over the iteration.
The space complexity of the above code is O(N), because we have created an extra string there to store the reverse of the given string.
Note ? Another approach can be implemented here but that uses very advanced data structures. The concept behind that approach is, we can start from the last character and will check until the first character meets. Then we can theoretically move that character to the last position and mark that position as achieved and store that value in the advanced data structure.
Then similarly for each character, we will find the same character present from behind which is not marked, and then increase the count by the total elements behind it minus the number of marked elements.
Conclusion
In this tutorial, we have implemented a code to find the minimum number of steps required to reverse the given string by swapping the adjacent characters only. We have used the nested while loop and reversed the copy of the given string to find the solution. The time complexity of the above code is O(N^2) where N is the size of the string and the space complexity is O(N).