Count of adjacent Vowel Consonant Pairs
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.
Examples:
Input : str = "bazeci" Output : 3 Input : str = "abu" Output : 1
Algorithm:
- 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:
C++
// C++ Program to implement the above approach #include <bits/stdc++.h> using namespace std; // Function to count the adjacent pairs of // consonant and vowels in the string int countPairs(string s) { // Using a set to store the vowels so that // checking each character becomes easier set<char> st; st.insert('a'); st.insert('e'); st.insert('i'); st.insert('o'); st.insert('u'); // Variable to store number of // consonant-vowel pairs int count = 0; int n = s.size(); for (int i = 0; i < n - 1; i++) { // If the ith character is not found in the set, // means it is a consonant // And if the (i+1)th character is found in the set, // means it is a vowel // We increment the count of such pairs if (st.find(s[i]) == st.end() && st.find(s[i + 1]) != st.end()) count++; } return count; } // Driver Code int main() { string s = "geeksforgeeks"; cout << countPairs(s); return 0; } |
Java
// Java Program to implement the above approach import java.util.*; class Sol { // Function to count the adjacent pairs of // consonant and vowels in the String static int countPairs(String s) { // Using a set to store the vowels so that // checking each character becomes easier Set<Character> st=new HashSet<Character>(); st.add('a'); st.add('e'); st.add('i'); st.add('o'); st.add('u'); // Variable to store number of // consonant-vowel pairs int count = 0; int n = s.length(); for (int i = 0; i < n - 1; i++) { // If the ith character is not found in the set, // means it is a consonant // And if the (i+1)th character is found in the set, // means it is a vowel // We increment the count of such pairs if (st.contains(s.charAt(i)) && !st.contains(s.charAt(i + 1))) count++; } return count; } // Driver Code public static void main(String args[]) { String s = "geeksforgeeks"; System.out.println( countPairs(s)); } } // This code is contributed by Arnab Kundu |
Python3
# Python3 Program to implement the above approach # Function to count the adjacent pairs of # consonant and vowels in the string def countPairs(s) : # Using a set to store the vowels so that # checking each character becomes easier st = set(); st.add('a'); st.add('e'); st.add('i'); st.add('o'); st.add('u'); # Variable to store number of # consonant-vowel pairs count = 0; n = len(s); for i in range(n - 1) : # If the ith character is not found in the set, # means it is a consonant # And if the (i+1)th character is found in the set, # means it is a vowel # We increment the count of such pairs if (s[i] not in st and s[i + 1] in st) : count += 1; return count; # Driver Code if __name__ == "__main__" : s = "geeksforgeeks"; print(countPairs(s)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { // Function to count the adjacent pairs of // consonant and vowels in the String static int countPairs(String s) { // Using a set to store the vowels so that // checking each character becomes easier HashSet<char> st = new HashSet<char>(); st.Add('a'); st.Add('e'); st.Add('i'); st.Add('o'); st.Add('u'); // Variable to store number of // consonant-vowel pairs int count = 0; int n = s.Length; for (int i = 0; i < n - 1; i++) { // If the ith character is not found in the set, // means it is a consonant // And if the (i+1)th character is found in the set, // means it is a vowel // We increment the count of such pairs if (st.Contains(s[i]) && !st.Contains(s[i + 1])) count++; } return count; } // Driver Code public static void Main(String[] args) { String s = "geeksforgeeks"; Console.Write( countPairs(s)); } } // This code has been contributed by 29AjayKumar |
3
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.
Recommended Posts:
- Alternate vowel and consonant string
- Program to find if a character is vowel or Consonant
- Lexicographically first alternate vowel and consonant string
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Program to count vowels, consonant, digits and special characters in string.
- Count the nodes of the tree whose weighted string contains a vowel
- Remove first adjacent pairs of similar characters until possible
- Count of strings where adjacent characters are of difference one
- Count pairs (i,j) such that (i+j) is divisible by A and B both
- Count the pairs of vowels in the given string
- Count number of equal pairs in a string
- Count of pairs of strings which differ in exactly one position
- Count pairs of strings that satisfy the given conditions
- Count pairs of characters in a string whose ASCII value difference is K
- Count of character pairs at same distance as in English alphabets
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



