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?
- CSS | caption-side Property
- Number of elements that can be seen from right side
- Number of Larger Elements on right side in a string
- Delete nodes which have a greater value on right side using recursion
- Remove first adjacent pairs of similar characters until possible
- 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
- Group consecutive characters of same type in a string
- Puzzle | Number of Sheets to be turned so that Prime Number has a Vowel on the other side
- Replace every element with the smallest element on its left side
- Replace every element with the greatest element on its left side
- Minimum Swaps for Bracket Balancing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.