Modify string by removing vowels in between two consonants
Last Updated :
26 Aug, 2022
Given a string S, comprising of only lowercase English alphabets, the task is to update the string by eliminating such vowels from the string that occur between two consonants.
Examples:
Input: bab
Output: bb
Here the letter ‘a’ is a vowel and is between two immediate consonants.
Thus, it is removed from the string, and the resultant string becomes ‘bb‘
Input: geeksforgeeks
Output: geeksfrgeeks
In the substring ‘for’ the alphabet ‘o’ is between two consonants ‘f’ and ‘r’.
Thus, it is removed from there and the string becomes-‘geeksfrgeeks‘
Approach: Initialize an empty updatedString. Given below are the steps to solve the above problem.
- Traverse the string from left to right.
- If the current character is a vowel, check the character before it and the character after it, if both of these are consonants, then the current vowel is a ‘Sandwiched Vowel’ and it needs to be removed from S, thus don’t append this character to A.
Else, append the current character to A.
- Continue the process until all the vowels in between two consonants are removed from the string
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' )
return true ;
else
return false ;
}
string updateSandwichedVowels(string a)
{
int n = a.length();
string updatedString = "" ;
for ( int i = 0; i < n; i++) {
if (!i || i == n - 1) {
updatedString += a[i];
continue ;
}
if (isVowel(a[i]) && !isVowel(a[i - 1])
&& !isVowel(a[i + 1])) {
continue ;
}
updatedString += a[i];
}
return updatedString;
}
int main()
{
string str = "geeksforgeeks" ;
string updatedString = updateSandwichedVowels(str);
cout << updatedString;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
static boolean isVowel( char x)
{
if (x == 'a' || x == 'e' ||
x == 'i' || x == 'o' ||
x == 'u' )
return true ;
else
return false ;
}
static String updateSandwichedVowels(String a)
{
int n = a.length();
String updatedString = "" ;
for ( int i = 0 ; i < n; i++)
{
if (i == 0 || i == n - 1 )
{
updatedString += a.charAt(i);
continue ;
}
if (isVowel(a.charAt(i))== true &&
isVowel(a.charAt(i - 1 ))== false &&
isVowel(a.charAt(i + 1 ))== false )
{
continue ;
}
updatedString += a.charAt(i);
}
return updatedString;
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
String updatedString = updateSandwichedVowels(str);
System.out.print(updatedString);
}
}
|
Python3
def isVowel(x):
if (x = = 'a' or x = = 'e' or x = = 'i' or
x = = 'o' or x = = 'u' ):
return True
else :
return False
def updateSandwichedVowels(a):
n = len (a)
updatedString = ""
for i in range ( 0 , n, 1 ):
if (i = = 0 or i = = n - 1 ):
updatedString + = a[i]
continue
if (isVowel(a[i]) = = True and
isVowel(a[i - 1 ]) = = False and
isVowel(a[i + 1 ]) = = False ):
continue
updatedString + = a[i]
return updatedString
if __name__ = = '__main__' :
str = "geeksforgeeks"
updatedString = updateSandwichedVowels( str )
print (updatedString)
|
C#
using System;
class GFG
{
static bool isVowel( char x)
{
if (x == 'a' || x == 'e' ||
x == 'i' || x == 'o' ||
x == 'u' )
return true ;
else
return false ;
}
static String updateSandwichedVowels(String a)
{
int n = a.Length;
String updatedString = "" ;
for ( int i = 0; i < n; i++)
{
if (i == 0 || i == n - 1)
{
updatedString += a[i];
continue ;
}
if ((isVowel(a[i])) == true &&
isVowel(a[i - 1]) == false &&
isVowel(a[i + 1]) == false )
{
continue ;
}
updatedString += a[i];
}
return updatedString;
}
public static void Main()
{
String str = "geeksforgeeks" ;
String updatedString = updateSandwichedVowels(str);
Console.WriteLine(updatedString);
}
}
|
PHP
<?php
function isVowel( $x )
{
if ( $x == 'a' || $x == 'e' ||
$x == 'i' || $x == 'o' || $x == 'u' )
return true;
else
return false;
}
function updateSandwichedVowels( $a )
{
$n = strlen ( $a );
$updatedString = "" ;
for ( $i = 0; $i < $n ; $i ++)
{
if (! $i || $i == $n - 1)
{
$updatedString .= $a [ $i ];
continue ;
}
if (isVowel( $a [ $i ]) && !isVowel( $a [ $i - 1]) &&
!isVowel( $a [ $i + 1]))
{
continue ;
}
$updatedString .= $a [ $i ];
}
return $updatedString ;
}
$str = "geeksforgeeks" ;
$updatedString = updateSandwichedVowels( $str );
echo $updatedString ;
?>
|
Javascript
<script>
function isVowel(x) {
if (x === "a" || x === "e" || x === "i" || x === "o" || x === "u" )
return true ;
else return false ;
}
function updateSandwichedVowels(a) {
var n = a.length;
var updatedString = "" ;
for ( var i = 0; i < n; i++) {
if (i === 0 || i === n - 1) {
updatedString += a[i];
continue ;
}
if (
isVowel(a[i]) === true &&
isVowel(a[i - 1]) === false &&
isVowel(a[i + 1]) === false
) {
continue ;
}
updatedString += a[i];
}
return updatedString;
}
var str = "geeksforgeeks" ;
var updatedString = updateSandwichedVowels(str);
document.write(updatedString);
</script>
|
Complexity Analysis:
- Time Complexity: O(N) where N is the length of the input string.
- Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...