Given a string, find minimum no of swaps(not necessarily adjacent) to convert it into a string which have similar characters side by side.
Input : abcb
Output : 1
Explanation : swap (c, b) to form abbc or acbb. Number of swap operations for this is 1;
Input : abbaacb
Output : 2
Explanation : Swap 0th index with 6th index and then swap 5th index with 6th index.
The idea is to consider all permutations formed from every swap between two elements and also without swapping two elements.
Time Complexity : The recurrence is T(n) = 2n*T(n-1) + O(n)
So the time complexity is greater than O((2*n)!)
- How to place two div side-by-side of the same height using CSS?
- Number of elements that can be seen from right side
- CSS | caption-side Property
- Number of Larger Elements on right side in a string
- Delete linked list nodes which have a Lesser Value on Left Side
- Delete linked list nodes which have a greater value on left side
- Group words with same set of characters
- Group all occurrences of characters according to first appearance
- Puzzle | Number of Sheets to be turned so that Prime Number has a Vowel on the other side
- Replace every element with the greatest element on its left side
- Replace every element with the smallest element on its left side
- Minimum Swaps for Bracket Balancing
- Minimum swaps required to convert one binary string to another
- Minimum swaps required to make a binary string alternating
- Check If every group of a's is followed by a group of b's of same length
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Rajput-Ji