# 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Algorithm:

1. We have to find all possible adjacent consonant-vowel pairs.
2. 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.
3. 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.
4. 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 ` `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 st=``new` `HashSet();  ` `    ``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 `

Output:

```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.

My Personal Notes arrow_drop_up Front and Back End Web Developer Android Developer Python and C++ Coder Networking Geek

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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.