给定一个字符串,任务是计算相邻对的数量,以使该对中的第一个元素为辅音,第二个元素为元音。找出对(i,i + 1)的对数,以使该字符串的第i个字符为辅音,第(i + 1)个字符为元音。
例子:
Input : str = "bazeci"
Output : 3
Input : str = "abu"
Output : 1
算法:
- 我们必须找到所有可能的相邻辅音元音对。
- 将所有元音插入集合或哈希中,以便我们可以在恒定时间内检查当前字符是元音还是辅音。
- 我们对前n-1个元素运行一个循环,并检查第i个字符是否是辅音,第(i + 1)个字符是元音。
- 如果是这样,我们将增加计数,否则我们将继续到字符串的末尾。
下面是上述方法的实现:
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 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 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[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
时间复杂度:O(N),其中N是字符串的长度。
辅助空间:O(1)。我们已经使用了额外的空间来将元音存储在哈希中,但是由于元音的数量只有5个,因此所使用的额外空间被认为是恒定的。