Minimum swaps required to move all vowels occurs after consonants in a given string

Given a string S, the task is to count the number of positions by which the vowels have to be moved such that all the consonants are placed at the front and all the vowels at the end. The order of consonants and vowels in the new string must be same.
 
Examples: 

Input: S = “abcdefghi”
Output: 9
Explanation:
The consonants present in the string are b, c, d, f, g and h and the vowels are a, e and i. On rearrangement the final string turns out to be “bcdfghaei” and the order of the consonants and vowels is not changed.
Initially ‘a’ was at index 0 and finally it moved to index 6. No. of positions moved = 6 – 0 = 6.
Initially ‘e’ was at index 4 and finally it moved to index 7. No. of positions moved = 7 – 4 = 3.
Initially ‘i’ was at index 8 and it didn’t change its position. So no. of moves = 0.
Total number of positions moved = 6 + 3 + 0 = 9.  

Input: S = “iijedf”
Output: 8
Explanation:
The consonants present in the string are j, d and f and the vowels are i, i and e. On rearrangement the final string turns out to be “jdfiie” and the order of the consonants and vowels is not changed.
‘i’ at index 0 is moved to index 3. No. of positions moved = 3 – 0 = 3.
‘i’ at index 1 is moved to index 4. No. of positions moved = 4 – 1 = 3.
‘e’ at index 3 is moved to index 5. No. of positions moved = 5 – 3 = 2.
Total number of positions moved = 3 + 3 + 2 = 8.

Approach: 

  1. Create the empty strings vowel and consonant to store the vowels and consonants of the given string.
  2. Traverse the given string S and if the current character is vowel then append it to the string vowel string else append is to the string consonant string.
  3. Store the concatenation of the strings consonant and vowel in the ans string.
  4. Initialize 2 pointers p1 and p2 such that p1 points to the 1st index of S and p2 points to the index where the first vowel appears in the ans string.
  5. Initialize a counter variable cnt to 0.
  6. Every time the character at index p1 matches with character at index p2, add the value of p2 – p1 to cnt and increment the values of p1 and p2 by 1.
  7. Repeat the step 6 for every index till the last index of ans is reached.

Below is the implementation of the above approach: 
 



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach 
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to check whether a character 
// is vowel or not 
bool isvowel(char x) 
    if (x == 'a' || x == 'e' || x == 'i'
        || x == 'o' || x == 'u' || x == 'A'
        || x == 'E' || x == 'I' || x == 'O'
        || x == 'U'
        return true
    else
        return false
  
// Function that creates a new string 
// such that all consonants are at 
// the front of the string 
void movetofront(string s) 
    // To store the vowels and 
    // consonants in the same order 
    string vowels, consonants; 
  
    // To store the resultant string 
    string ans; 
  
    vowels = consonants = ans = ""
  
    for (int i = 0; s[i]; i++) { 
  
        // Check if s[i] is vowel 
        if (isvowel(s[i])) { 
            vowels += s[i]; 
        
  
        // Else s[i] is consonant 
        else
            consonants += s[i]; 
        
    
  
    // concatenate the strings formed 
    ans = consonants + vowels; 
  
    // Pointer variables 
    int p1 = 0; 
    int p2 = consonants.size(); 
  
    // Counter variable 
    int cnt = 0; 
  
    // Condition to check if the 
    // given string has only 
    // consonants 
    if (p2 == ans.size()) { 
        cout << 0 << endl; 
        return
    
  
    // Condition to check if the 
    // string has only vowels 
    if (ans.size() == vowels.size()) { 
        cout << 0 << endl; 
        return
    
  
    // Loop to find the count of 
    // number of positions moved 
    while (p2 < ans.size()) { 
        if (ans[p2] == s[p1]) { 
            cnt += p2 - p1; 
            p1++; 
            p2++; 
        
        else
            p1++; 
        
    
    cout << cnt << endl; 
    return
  
// Driver Code 
int main() 
    // Given string 
    string s = "abcdefghi"
  
    // Function Call 
    movetofront(s); 
    return 0; 
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach 
import java.util.*; 
class GFG{ 
  
// Function to check whether a character 
// is vowel or not 
static boolean isvowel(char x) 
    if (x == 'a' || x == 'e' || x == 'i' || 
        x == 'o' || x == 'u' || x == 'A' || 
        x == 'E' || x == 'I' || x == 'O' || 
        x == 'U'
        return true
    else
        return false
  
// Function that creates a new String 
// such that all consonants are at 
// the front of the String 
static void movetofront(String s) 
    // To store the vowels and 
    // consonants in the same order 
    String vowels, consonants; 
  
    // To store the resultant String 
    String ans; 
  
    vowels = consonants = ans = ""
  
    for (int i = 0; i < s.length(); i++) 
    
  
        // Check if s.charAt(i) is vowel 
        if (isvowel(s.charAt(i))) 
        
            vowels += s.charAt(i); 
        
  
        // Else s.charAt(i) is consonant 
        else
        
            consonants += s.charAt(i); 
        
    
  
    // concatenate the Strings formed 
    ans = consonants + vowels; 
  
    // Pointer variables 
    int p1 = 0
    int p2 = consonants.length(); 
  
    // Counter variable 
    int cnt = 0
  
    // Condition to check if the 
    // given String has only 
    // consonants 
    if (p2 == ans.length()) 
    
        System.out.print(0 + "\n"); 
        return
    
  
    // Condition to check if the 
    // String has only vowels 
    if (ans.length() == vowels.length()) 
    
        System.out.print(0 + "\n"); 
        return
    
  
    // Loop to find the count of 
    // number of positions moved 
    while (p2 < ans.length()) 
    
        if (ans.charAt(p2) == s.charAt(p1)) 
        
            cnt += p2 - p1; 
            p1++; 
            p2++; 
        
        else
        
            p1++; 
        
    
    System.out.print(cnt + "\n"); 
    return
  
// Driver Code 
public static void main(String[] args) 
    // Given String 
    String s = "abcdefghi"
  
    // Function Call 
    movetofront(s); 
  
// This code is contributed by sapnasingh4991 
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Function to check whether a character 
# is vowel or not 
def isvowel(x): 
  
    if (x == 'a' or x == 'e' or 
        x == 'i' or x == 'o' or
        x == 'u' or x == 'A' or
        x == 'E' or x == 'I' or
        x == 'O' or x == 'U'): 
        return bool(True
    else:
        return bool(False)
  
# Function that creates a new string 
# such that all consonants are at 
# the front of the string 
def movetofront(s): 
  
    # To store the vowels and 
    # consonants in the same order 
    vowels = consonants = ans = "" 
      
    for i in range(len(s)):
  
        # Check if s[i] is vowel 
        if (isvowel(s[i])): 
            vowels += s[i]
  
        # Else s[i] is consonant 
        else:
            consonants += s[i]
  
    # concatenate the strings formed 
    ans = consonants + vowels
  
    # Pointer variables 
    p1 = 0
    p2 = len(consonants) 
  
    # Counter variable 
    cnt = 0
  
    # Condition to check if the 
    # given string has only 
    # consonants 
    if (p2 == len(ans)):
        print(0)
        return
  
    # Condition to check if the 
    # string has only vowels 
    if (len(ans) == len(vowels)): 
        print(0)
        return
  
    # Loop to find the count of 
    # number of positions moved 
    while (p2 < len(ans)): 
        if (ans[p2] == s[p1]): 
            cnt += p2 - p1
            p1 += 1
            p2 += 1
        else:
            p1 += 1
              
    print(cnt) 
    return
  
# Driver code
  
# Given string 
s = "abcdefghi"
  
# Function call 
movetofront(s)
  
# This code is contributed by divyeshrabadiya07
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach 
using System; 
class GFG{ 
  
// Function to check whether a character 
// is vowel or not 
static bool isvowel(char x) 
    if (x == 'a' || x == 'e' || x == 'i' || 
        x == 'o' || x == 'u' || x == 'A' || 
        x == 'E' || x == 'I' || x == 'O' || 
        x == 'U'
        return true
    else
        return false
  
// Function that creates a new String 
// such that all consonants are at 
// the front of the String 
static void movetofront(String s) 
    // To store the vowels and 
    // consonants in the same order 
    String vowels, consonants; 
  
    // To store the resultant String 
    String ans; 
  
    vowels = consonants = ans = ""
  
    for (int i = 0; i < s.Length; i++) 
    
  
        // Check if s[i] is vowel 
        if (isvowel(s[i])) 
        
            vowels += s[i]; 
        
  
        // Else s[i] is consonant 
        else
        
            consonants += s[i]; 
        
    
  
    // concatenate the Strings formed 
    ans = consonants + vowels; 
  
    // Pointer variables 
    int p1 = 0; 
    int p2 = consonants.Length; 
  
    // Counter variable 
    int cnt = 0; 
  
    // Condition to check if the 
    // given String has only 
    // consonants 
    if (p2 == ans.Length) 
    
        Console.Write(0 + "\n"); 
        return
    
  
    // Condition to check if the 
    // String has only vowels 
    if (ans.Length == vowels.Length) 
    
        Console.Write(0 + "\n"); 
        return
    
  
    // Loop to find the count of 
    // number of positions moved 
    while (p2 < ans.Length) 
    
        if (ans[p2] == s[p1]) 
        
            cnt += p2 - p1; 
            p1++; 
            p2++; 
        
        else
        
            p1++; 
        
    
    Console.Write(cnt + "\n"); 
    return
  
// Driver Code 
public static void Main(String[] args) 
    // Given String 
    String s = "abcdefghi"
  
    // Function Call 
    movetofront(s); 
  
// This code is contributed by sapnasingh4991 
chevron_right

Output:
9

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N), where N is the length of the string.

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.




Article Tags :
Practice Tags :