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)!)
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.
- Check If every group of a's is followed by a group of b's of same length
- How to place two div side-by-side of the same height using CSS?
- Remove first adjacent pairs of similar characters until possible
- Maximize cost of deletions to obtain string having no pair of similar adjacent characters
- Group all occurrences of characters according to first appearance
- Group words with same set of characters
- Group consecutive characters of same type in a string
- Minimum Swaps for Bracket Balancing
- Minimum swaps required to convert one binary string to another
- Minimum swaps required to make a binary string alternating
- Minimum number of adjacent swaps to convert a string into its given anagram
- Minimum number of swaps to make two binary string equal
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum swaps to make two strings equal by swapping only with third string
- Minimum number of swaps required to make the string K periodic
- Minimum swaps required to move all vowels occurs after consonants in a given string
- Minimum number of operations to move all uppercase characters before all lower case characters
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Minimum characters to be replaced to make frequency of all characters same
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
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.