Dada una string, la tarea es contar el número de pares adyacentes de modo que el primer elemento del par sea una consonante y el segundo elemento sea una vocal. Es decir, encuentre el número de pares (i, i+1) tales que el i-ésimo carácter de esta string sea una consonante y el (i+1)-ésimo carácter sea una vocal.
Ejemplos:
Input : str = "bazeci" Output : 3 Input : str = "abu" Output : 1
Algoritmo :
- Tenemos que encontrar todos los posibles pares de consonantes-vocales adyacentes.
- Inserte todas las vocales en un conjunto o hash, para que podamos verificar si el carácter actual es una vocal o una consonante en tiempo constante.
- Ejecutamos un bucle para los primeros n-1 elementos y comprobamos si el i-ésimo carácter es una consonante y el (i+1)-ésimo carácter una vocal o no.
- Si es así, incrementamos el conteo, de lo contrario continuamos hasta el final de la string.
A continuación se muestra la implementación del enfoque anterior:
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
Javascript
<script> // Javascript program for the above approach // Function to count the adjacent pairs of // consonant and vowels in the String function countPairs(s) { // Using a set to store the vowels so that // checking each character becomes easier let st=new Set(); st.add('a'); st.add('e'); st.add('i'); st.add('o'); st.add('u'); // Variable to store number of // consonant-vowel pairs let count = 0; let n = s.length; for (let 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.has(s[i]) && !st.has(s[i + 1])) count++; } return count; } // Driver Code let s = "geeksforgeeks"; document.write( countPairs(s)); // This code is contributed by sanjoy_62. </script>
Producción:
3
Complejidad de tiempo : O(N), donde N es la longitud de la string.
Espacio Auxiliar : O(1). Hemos utilizado espacio adicional para almacenar vocales en un hash, pero dado que el número de vocales es solo 5, el espacio adicional utilizado se considera constante.