Given a string, the task is to count the number of adjacent pairs such that the first element of the pair is a consonant and the second element is a vowel. That is find the number of pairs (i, i+1) such that the ith character of this string is a consonant and the (i+1)th character is a vowel.
Input : str = "bazeci" Output : 3 Input : str = "abu" Output : 1
- We have to find all possible adjacent consonant-vowel pairs.
- Insert all of the vowels in a set or hash, so that we can check if the current character is a vowel or consonant in constant time.
- We run a loop for the first n-1 elements and check, if the ith character is a consonant, and the (i+1)th character a vowel or not.
- If so, we increment the count, else we continue till the end of the string.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1). We have used additional space to store vowels in a Hash but since number of vowels is only 5 so, the extra space used is considered as constant.
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.
- Count of N size strings consisting of at least one vowel and one consonant
- Alternate vowel and consonant string
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Lexicographically first alternate vowel and consonant string
- Program to find if a character is vowel or Consonant
- Program to count vowels, consonant, digits and special characters in string.
- Replace every consonant sequence with its length in the given string
- Count pair of strings whose concatenation has every vowel
- Count the nodes of the tree whose weighted string contains a vowel
- Substrings starting with vowel and ending with consonants and vice versa
- Arrangement of words without changing the relative position of vowel and consonants
- Python program to check if given string is vowel Palindrome
- Check if the given string is vowel prime
- Program to accept Strings starting with a Vowel
- Replace every vowels with lexicographically next vowel in a String
- Minimize cost to replace all the vowels of a given String by a single vowel
- Minimum string such that every adjacent character of given string is still adjacent
- Remove first adjacent pairs of similar characters until possible
- Smallest number possible by swapping adjacent even odd pairs
- Minimize length of string by replacing K pairs of distinct adjacent characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.