Minimum Swaps: Geek has a string S consisting of lowercase english alphabet characters. Geek wants string to look beautiful which is only possible when all the vowels present in the string forms a consecutive chain of characters in the string. Your task is to find the minimum swaps required to make string beautiful.
Example 1:
Input: S = "ababab" Output: 1 Explanation: We will swap second character of the string with fifth character of the string which will make string beautiful. Example 2:
Input: S = "aaabbb" Output: 0 Explanation: The string is already beautiful. Your Task: You don't need to read input or print anything. Your task is to complete the function minSwaps( ) which takes string S as an input parameter and returns the number of minimum swaps required.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 ≤ |S| ≤ 2*105
Solution
def minSwaps(self,s):
size = len(s)
arrpos = [*enumerate(s)]
arrpos.sort(key=lambda x: x[1])
vis = {k: False for k in range(size)}
ans = 0
for i in range(size):
if vis[i] or arrpos[i][0] == i:
continue
j = i
while not vis[j]:
vis[j] = True
j = arrpos[j][0]
ans += 1
return ansHappy Learning – If you require any further information, feel free to contact me.
![[Solved] find the minimum swaps required to make string beautiful GFG Contest [Solved] find the minimum swaps required to make string beautiful GFG Contest](https://realcoder.techss24.com/wp-content/uploads/2022/06/Solved-find-the-minimum-swaps-required-to-make-string-beautiful-GFG-Contest.png)

![[Solved] Develop a function in python to generate and sum the following series](https://realcoder.techss24.com/wp-content/uploads/2022/07/Solved-Develop-a-function-in-python-to-generate-and-sum-the-following-series-300x200.png)
![[Solved] Shyam has N Jars of Ladoos and he wants to distribute the Ladoos amongst M Villagers with Python](https://realcoder.techss24.com/wp-content/uploads/2022/10/Solved-delete-the-element-from-the-queue-and-insert-that-element-again-in-the-queue-2-300x200.png)