Count of adjacent Vowel Consonant Pairs
Last Updated :
02 Jun, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int countPairs(string s)
{
set< char > st;
st.insert( 'a' );
st.insert( 'e' );
st.insert( 'i' );
st.insert( 'o' );
st.insert( 'u' );
int count = 0;
int n = s.size();
for ( int i = 0; i < n - 1; i++) {
if (st.find(s[i]) == st.end() && st.find(s[i + 1]) != st.end())
count++;
}
return count;
}
int main()
{
string s = "geeksforgeeks" ;
cout << countPairs(s);
return 0;
}
|
Java
import java.util.*;
class Sol
{
static int countPairs(String s)
{
Set<Character> st= new HashSet<Character>();
st.add( 'a' );
st.add( 'e' );
st.add( 'i' );
st.add( 'o' );
st.add( 'u' );
int count = 0 ;
int n = s.length();
for ( int i = 0 ; i < n - 1 ; i++)
{
if (st.contains(s.charAt(i)) && !st.contains(s.charAt(i + 1 )))
count++;
}
return count;
}
public static void main(String args[])
{
String s = "geeksforgeeks" ;
System.out.println( countPairs(s));
}
}
|
Python3
def countPairs(s) :
st = set ();
st.add( 'a' );
st.add( 'e' );
st.add( 'i' );
st.add( 'o' );
st.add( 'u' );
count = 0 ;
n = len (s);
for i in range (n - 1 ) :
if (s[i] not in st and s[i + 1 ] in st) :
count + = 1 ;
return count;
if __name__ = = "__main__" :
s = "geeksforgeeks" ;
print (countPairs(s));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countPairs(String s)
{
HashSet< char > st = new HashSet< char >();
st.Add( 'a' );
st.Add( 'e' );
st.Add( 'i' );
st.Add( 'o' );
st.Add( 'u' );
int count = 0;
int n = s.Length;
for ( int i = 0; i < n - 1; i++)
{
if (st.Contains(s[i]) && !st.Contains(s[i + 1]))
count++;
}
return count;
}
public static void Main(String[] args)
{
String s = "geeksforgeeks" ;
Console.Write( countPairs(s));
}
}
|
Javascript
<script>
function countPairs(s)
{
let st= new Set();
st.add( 'a' );
st.add( 'e' );
st.add( 'i' );
st.add( 'o' );
st.add( 'u' );
let count = 0;
let n = s.length;
for (let i = 0; i < n - 1; i++)
{
if (st.has(s[i]) && !st.has(s[i + 1]))
count++;
}
return count;
}
let s = "geeksforgeeks" ;
document.write( countPairs(s));
</script>
|
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.
Share your thoughts in the comments
Please Login to comment...