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:
- Create the empty strings vowel and consonant to store the vowels and consonants of the given string.
- 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.
- Store the concatenation of the strings consonant and vowel in the ans string.
- 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.
- Initialize a counter variable cnt to 0.
- 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.
- Repeat the step 6 for every index till the last index of ans is reached.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
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 ;
}
void movetofront(string s)
{
string vowels, consonants;
string ans;
vowels = consonants = ans = "" ;
for ( int i = 0; s[i]; i++) {
if (isvowel(s[i])) {
vowels += s[i];
}
else {
consonants += s[i];
}
}
ans = consonants + vowels;
int p1 = 0;
int p2 = consonants.size();
int cnt = 0;
if (p2 == ans.size()) {
cout << 0 << endl;
return ;
}
if (ans.size() == vowels.size()) {
cout << 0 << endl;
return ;
}
while (p2 < ans.size()) {
if (ans[p2] == s[p1]) {
cnt += p2 - p1;
p1++;
p2++;
}
else {
p1++;
}
}
cout << cnt << endl;
return ;
}
int main()
{
string s = "abcdefghi" ;
movetofront(s);
return 0;
}
|
Java
import java.util.*;
class GFG{
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 ;
}
static void movetofront(String s)
{
String vowels, consonants;
String ans;
vowels = consonants = ans = "" ;
for ( int i = 0 ; i < s.length(); i++)
{
if (isvowel(s.charAt(i)))
{
vowels += s.charAt(i);
}
else
{
consonants += s.charAt(i);
}
}
ans = consonants + vowels;
int p1 = 0 ;
int p2 = consonants.length();
int cnt = 0 ;
if (p2 == ans.length())
{
System.out.print( 0 + "\n" );
return ;
}
if (ans.length() == vowels.length())
{
System.out.print( 0 + "\n" );
return ;
}
while (p2 < ans.length())
{
if (ans.charAt(p2) == s.charAt(p1))
{
cnt += p2 - p1;
p1++;
p2++;
}
else
{
p1++;
}
}
System.out.print(cnt + "\n" );
return ;
}
public static void main(String[] args)
{
String s = "abcdefghi" ;
movetofront(s);
}
}
|
Python3
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 )
def movetofront(s):
vowels = consonants = ans = ""
for i in range ( len (s)):
if (isvowel(s[i])):
vowels + = s[i]
else :
consonants + = s[i]
ans = consonants + vowels
p1 = 0
p2 = len (consonants)
cnt = 0
if (p2 = = len (ans)):
print ( 0 )
return
if ( len (ans) = = len (vowels)):
print ( 0 )
return
while (p2 < len (ans)):
if (ans[p2] = = s[p1]):
cnt + = p2 - p1
p1 + = 1
p2 + = 1
else :
p1 + = 1
print (cnt)
return
s = "abcdefghi"
movetofront(s)
|
C#
using System;
class GFG{
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 ;
}
static void movetofront(String s)
{
String vowels, consonants;
String ans;
vowels = consonants = ans = "" ;
for ( int i = 0; i < s.Length; i++)
{
if (isvowel(s[i]))
{
vowels += s[i];
}
else
{
consonants += s[i];
}
}
ans = consonants + vowels;
int p1 = 0;
int p2 = consonants.Length;
int cnt = 0;
if (p2 == ans.Length)
{
Console.Write(0 + "\n" );
return ;
}
if (ans.Length == vowels.Length)
{
Console.Write(0 + "\n" );
return ;
}
while (p2 < ans.Length)
{
if (ans[p2] == s[p1])
{
cnt += p2 - p1;
p1++;
p2++;
}
else
{
p1++;
}
}
Console.Write(cnt + "\n" );
return ;
}
public static void Main(String[] args)
{
String s = "abcdefghi" ;
movetofront(s);
}
}
|
Javascript
<script>
function isvowel(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 movetofront(s) {
var vowels, consonants;
var ans;
vowels = consonants = ans = "" ;
for ( var i = 0; i < s.length; i++) {
if (isvowel(s[i])) {
vowels += s[i];
}
else {
consonants += s[i];
}
}
ans = consonants + vowels;
var p1 = 0;
var p2 = consonants.length;
var cnt = 0;
if (p2 === ans.length) {
document.write(0 + "<br>" );
return ;
}
if (ans.length === vowels.length) {
document.write(0 + "<br>" );
return ;
}
while (p2 < ans.length) {
if (ans[p2] === s[p1]) {
cnt += p2 - p1;
p1++;
p2++;
} else {
p1++;
}
}
document.write(cnt + "<br>" );
return ;
}
var s = "abcdefghi" ;
movetofront(s);
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N), where N is the length of the string.
Last Updated :
12 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...