Remove consecutive vowels from string
Given a string s of lowercase letters, we need to remove consecutive vowels from the string
Note : Sentence should not contain two consecutive vowels ( a, e, i, o, u).
Examples :
Input: geeks for geeks Output: geks for geks Input : your article is in queue Output : yor article is in qu
Approach: Iterate string using a loop and check for the repetitiveness of vowels in a given sentence and in case if consecutive vowels are found then delete the vowel till coming next consonant and printing the updated string.
Implementation:
C++
// C++ program for printing sentence // without repetitive vowels #include <bits/stdc++.h> using namespace std; // function which returns True or False // for occurrence of a vowel bool is_vow( char c) { // this compares vowel with // character 'c' return (c == 'a' ) || (c == 'e' ) || (c == 'i' ) || (c == 'o' ) || (c == 'u' ); } // function to print resultant string void removeVowels(string str) { // print 1st character printf ( "%c" , str[0]); // loop to check for each character for ( int i = 1; str[i]; i++) // comparison of consecutive characters if ((!is_vow(str[i - 1])) || (!is_vow(str[i]))) printf ( "%c" , str[i]); } // Driver Code int main() { char str[] = " geeks for geeks" ; removeVowels(str); } // This code is contributed by Abhinav96 |
Java
// Java program for printing sentence // without repetitive vowels import java.io.*; import java.util.*; import java.lang.*; class GFG { // function which returns // True or False for // occurrence of a vowel static boolean is_vow( char c) { // this compares vowel // with character 'c' return (c == 'a' ) || (c == 'e' ) || (c == 'i' ) || (c == 'o' ) || (c == 'u' ); } // function to print // resultant string static void removeVowels(String str) { // print 1st character System.out.print(str.charAt( 0 )); // loop to check for // each character for ( int i = 1 ; i < str.length(); i++) // comparison of // consecutive characters if ((!is_vow(str.charAt(i - 1 ))) || (!is_vow(str.charAt(i)))) System.out.print(str.charAt(i)); } // Driver Code public static void main(String[] args) { String str = "geeks for geeks" ; removeVowels(str); } } |
Python3
# Python3 implementation for printing # sentence without repetitive vowels # function which returns True or False # for occurrence of a vowel def is_vow(c): # this compares vowel with # character 'c' return ((c = = 'a' ) or (c = = 'e' ) or (c = = 'i' ) or (c = = 'o' ) or (c = = 'u' )); # function to print resultant string def removeVowels( str ): # print 1st character print ( str [ 0 ], end = ""); # loop to check for each character for i in range ( 1 , len ( str )): # comparison of consecutive # characters if ((is_vow( str [i - 1 ]) ! = True ) or (is_vow( str [i]) ! = True )): print ( str [i], end = ""); # Driver code str = " geeks for geeks" ; removeVowels( str ); # This code is contributed by mits |
C#
// C# program for printing sentence // without repetitive vowels using System; class GFG { // function which returns // True or False for // occurrence of a vowel static bool is_vow( char c) { // this compares vowel // with character 'c' return (c == 'a' ) || (c == 'e' ) || (c == 'i' ) || (c == 'o' ) || (c == 'u' ); } // function to print // resultant string static void removeVowels( string str) { // print 1st character Console.Write(str[0]); // loop to check for // each character for ( int i = 1; i < str.Length; i++) // comparison of // consecutive characters if ((!is_vow(str[i - 1])) || (!is_vow(str[i]))) Console.Write(str[i]); } // Driver Code static void Main() { string str = "geeks for geeks" ; removeVowels(str); } } // This code is contributed // by Manish Shaw(manishshaw1) |
PHP
<?php // PHP implementation for printing // sentence without repetitive vowels // function which returns True or False // for occurrence of a vowel function is_vow( $c ) { // this compares vowel with // character 'c' return ( $c == 'a' ) || ( $c == 'e' ) || ( $c == 'i' ) || ( $c == 'o' ) || ( $c == 'u' ); } // function to print resultant string function removeVowels( $str ) { // print 1st character printf( $str [0]); // loop to check for each character for ( $i = 1; $i < strlen ( $str ); $i ++) // comparison of consecutive // characters if ((!is_vow( $str [ $i - 1])) || (!is_vow( $str [ $i ]))) printf( $str [ $i ]); } // Driver code $str = " geeks for geeks" ; removeVowels( $str ); // This code is contributed by mits ?> |
Javascript
<script> // JavaScript program for printing sentence // without repetitive vowels // function which returns True or False // for occurrence of a vowel function is_vow(c) { // this compares vowel with // character 'c' return (c == 'a' ) || (c == 'e' ) || (c == 'i' ) || (c == 'o' ) || (c == 'u' ); } // function to print resultant string function removeVowels(str) { // print 1st character document.write(str[0]); // loop to check for each character for (let i = 1; i<str.length; i++) // comparison of consecutive characters if ((!is_vow(str[i - 1])) || (!is_vow(str[i]))) document.write(str[i]); } // Driver Code let str = " geeks for geeks" ; removeVowels(str); // This code is contributed by shinjanpatra </script> |
geks for geks
Time Complexity: O(n), where n is the length of the string
Space Complexity: O(n), where n is the length of the string
Another approach :- here’s another approach in C++ to remove consecutive vowels from a string using a stack:
Start the program by including the required header files and the standard namespace.
Define a function named isVowel that takes a character as input and returns a boolean value indicating whether the character is a vowel.
Define a function named removeConsecutiveVowels that takes a string as input and returns a string with all consecutive vowels removed.
Create a stack named stk to store the characters of the input string.
Get the length of the input string.
Loop through each character of the input string by using a for loop.
Check if the current character is a vowel by calling the isVowel function.
If the current character is a vowel, check if the stack is not empty and the top of the stack is also a vowel.
If the conditions in step 8 are satisfied, pop all consecutive vowels from the stack.
Push the current character onto the stack.
Construct the result string by popping all elements from the stack.
Return the result string.
Define the main function, which sets the input string to ” geeks for geeks”, calls the removeConsecutiveVowels function with this string, and outputs the result.
End the program by returning 0 from the main function.
C++
#include <iostream> #include <string> #include <stack> using namespace std; bool isVowel( char c) { // check if a character is a vowel return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ); } string removeConsecutiveVowels(string str) { stack< char > stk; int len = str.length(); for ( int i = 0; i < len; i++) { // if current character is a vowel if (isVowel(str[i])) { // check if the stack is not empty and the top of the stack is also a vowel if (!stk.empty() && isVowel(stk.top())) { // pop all consecutive vowels from the stack while (!stk.empty() && isVowel(stk.top())) { stk.pop(); } } } // push the current character onto the stack stk.push(str[i]); } // construct the result string by popping all elements from the stack string result = "" ; while (!stk.empty()) { result = stk.top() + result; stk.pop(); } return result; } int main() { string str = " geeks for geeks" ; cout << removeConsecutiveVowels(str) << endl; // expected output: "ltcdsccmmntyfrcdrs" return 0; } |
C#
using System; using System.Collections.Generic; public class Program { static bool IsVowel( char c) { // check if a character is a vowel return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' ); } static string RemoveConsecutiveVowels( string str) { Stack< char > stk = new Stack< char >(); int len = str.Length; for ( int i = 0; i < len; i++) { // if current character is a vowel if (IsVowel(str[i])) { // check if the stack is not empty and the // top of the stack is also a vowel if (stk.Count > 0 && IsVowel(stk.Peek())) { // pop all consecutive vowels from the // stack while (stk.Count > 0 && IsVowel(stk.Peek())) { stk.Pop(); } } } // push the current character onto the stack stk.Push(str[i]); } // construct the result string by popping all // elements from the stack string result = "" ; while (stk.Count > 0) { result = stk.Peek() + result; stk.Pop(); } return result; } public static void Main() { string str = " geeks for geeks" ; Console.WriteLine(RemoveConsecutiveVowels( str)); // expected output: " gks fr gks" } } // This code is contributed by user_dtewbxkn77n |
Javascript
function isVowel(c) { // check if a character is a vowel return (c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u' || c === 'A' || c === 'E' || c === 'I' || c === 'O' || c === 'U' ); } function removeConsecutiveVowels(str) { let stk = []; let len = str.length; for (let i = 0; i < len; i++) { // if current character is a vowel if (isVowel(str[i])) { // check if the stack is not empty and the top of the stack is also a vowel if (stk.length > 0 && isVowel(stk[stk.length - 1])) { // pop all consecutive vowels from the stack while (stk.length > 0 && isVowel(stk[stk.length - 1])) { stk.pop(); } } } // push the current character onto the stack stk.push(str[i]); } // construct the result string by popping all elements from the stack let result = "" ; while (stk.length > 0) { result = stk[stk.length - 1] + result; stk.pop(); } return result; } let str = " geeks for geeks" ; console.log(removeConsecutiveVowels(str)); |
geks for geks
Time Complexity: O(n), where n is the length of the string
The time complexity of the removeConsecutiveVowels function is O(n), where n is the length of the input string. This is because each character of the input string is processed once in the for loop, and all operations inside the loop are constant time operations.
Space Complexity: O(n), where n is the length of the string
The space complexity of the function is O(n), where n is the length of the input string. This is because the size of the stack can be at most the length of the input string, and the result string can also be of the same size as the input string in the worst case.
Please Login to comment...