Count the pairs of vowels in the given string
Last Updated :
08 Jun, 2022
Given a string str consisting of lowercase English alphabets, the task is to count the number of adjacent pairs of vowels.
Examples:
Input: str = “abaebio”
Output: 2
(a, e) and (i, o) are the only valid pairs.
Input: str = “aeoui”
Output: 4
Approach: Starting from the first character of the string to the second last character, increment count for every character where str[i] as well as str[i + 1] are both vowels. Print the count in the end which is the required count of pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char ch)
{
switch (ch) {
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
return true ;
default :
return false ;
}
}
int vowelPairs(string s, int n)
{
int cnt = 0;
for ( int i = 0; i < n - 1; i++) {
if (isVowel(s[i]) && isVowel(s[i + 1]))
cnt++;
}
return cnt;
}
int main()
{
string s = "abaebio" ;
int n = s.length();
cout << vowelPairs(s, n);
return 0;
}
|
Java
class GFG {
static boolean isVowel( char ch)
{
switch (ch) {
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
return true ;
default :
return false ;
}
}
static int vowelPairs(String s, int n)
{
int cnt = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
if (isVowel(s.charAt(i)) && isVowel(s.charAt(i + 1 )))
cnt++;
}
return cnt;
}
public static void main(String args[])
{
String s = "abaebio" ;
int n = s.length();
System.out.print(vowelPairs(s, n));
}
}
|
Python3
def isVowel(ch):
if ch in [ 'a' , 'e' , 'i' , 'o' , 'u' ]:
return True
else :
return False
def vowelPairs(s, n):
cnt = 0
for i in range (n - 1 ):
if (isVowel(s[i]) and
isVowel(s[i + 1 ])):
cnt + = 1
return cnt
s = "abaebio"
n = len (s)
print (vowelPairs(s, n))
|
C#
using System;
class GFG
{
static bool isVowel( char ch)
{
switch (ch)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
return true ;
default :
return false ;
}
}
static int vowelPairs( string s, int n)
{
int cnt = 0;
for ( int i = 0; i < n - 1; i++)
{
if (isVowel(s[i]) && isVowel(s[i + 1]))
cnt++;
}
return cnt;
}
public static void Main()
{
string s = "abaebio" ;
int n = s.Length;
Console.WriteLine(vowelPairs(s, n));
}
}
|
PHP
<?php
function isVowel( $ch )
{
if ( $ch == 'a' || $ch == 'e' ||
$ch == 'i' || $ch == 'o' ||
$ch == 'u' )
return true;
return false;
}
function vowelPairs( $s , $n )
{
$cnt = 0;
for ( $i = 0; $i < $n - 1; $i ++)
{
if (isVowel( $s [ $i ]) &&
isVowel( $s [ $i + 1]))
$cnt ++;
}
return $cnt ;
}
$s = "abaebio" ;
$n = strlen ( $s );
echo vowelPairs( $s , $n );
?>
|
Javascript
<script>
function isVowel(ch)
{
switch (ch) {
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
return true ;
default :
return false ;
}
}
function vowelPairs(s, n)
{
let cnt = 0;
for (let i = 0; i < n - 1; i++) {
if (isVowel(s[i]) && isVowel(s[i + 1]))
cnt++;
}
return cnt;
}
let s = "abaebio" ;
let n = s.length;
document.write(vowelPairs(s, n));
</script>
|
Time Complexity: O(n) where n is the length of the string
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...