Dada la string str de tamaño N que contiene letras mayúsculas y minúsculas, y dos enteros K y X . La tarea es encontrar el recuento de substrings de tamaño K que contengan como máximo X vocales distintas.
Ejemplos:
Entrada: str = «TrueGoik», K = 3, X = 2
Salida: 6
Explicación: Las cinco substrings son «Tru», «rue», «ueG», «eGo», «Goi» y «oik».Entrada: str = “GeeksForGeeks”, K = 2, X = 2
Salida: 12
Explicación: “Ge”, “ee”, “ek”, “ks”, “sf”, “fo”, “or”, “rG ”, “Ge”, “ee”, “ek” y “ks”.
Enfoque: Para resolver el problema, primero hay que generar todas las substrings de longitud K. Luego, en cada substring, verifique si el número de vocales distintas es menor que X o no. Siga los pasos que se mencionan a continuación.
- Primero genere todas las substrings de longitud K a partir de cada índice i en [0, N – K].
- Luego, para cada substring de longitud K , haga lo siguiente:
- Mantenga un hash para almacenar las apariciones de vocales únicas.
- Compruebe si un nuevo carácter en la substring es una vocal o no.
- Si es una vocal, incremente su ocurrencia en el hash y mantenga un conteo de vocales distintas encontradas
- Ahora, para cada substring, si el conteo distinto de vocales es menor o igual a X , incremente el conteo final.
- Cuando se hayan considerado todas las substrings, imprima el recuento final.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ code to implement above approach #include <bits/stdc++.h> using namespace std; #define MAX 128 // Function to check whether // a character is vowel or not bool isVowel(char x) { return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U'); } int getIndex(char ch) { return (ch - 'A' > 26 ? ch - 'a' : ch - 'A'); } // Function to find the count of K length // substring with at most x distinct vowels int get(string str, int k, int x) { int n = str.length(); // Initialize result int res = 0; // Consider all substrings // beginning with str[i] for (int i = 0; i <= n - k; i++) { int dist_count = 0; // To store count of characters // from 'a' to 'z' vector<int> cnt(26, 0); // Consider all substrings // between str[i..j] for (int j = i; j < i + k; j++) { // If this is a new vowels // for this substring, // increment dist_count. if (isVowel(str[j]) && cnt[getIndex(str[j])] == 0) { dist_count++; } // Increment count of // current character cnt[getIndex(str[j])]++; } // If count of distinct vowels // in current substring // of length k is less than // equal to x, then increment result. if (dist_count <= x) res++; } return res; } // Driver code int main(void) { string s = "TrueGoik"; int K = 3, X = 2; cout << get(s, K, X); return 0; }
Java
// Java code to implement above approach import java.util.Arrays; class GFG { static int MAX = 128; // Function to check whether // a character is vowel or not static boolean isVowel(char x) { return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U'); } static int getIndex(char ch) { return (ch - 'A' > 26 ? ch - 'a' : ch - 'A'); } // Function to find the count of K length // substring with at most x distinct vowels static int get(String str, int k, int x) { int n = str.length(); // Initialize result int res = 0; // Consider all substrings // beginning with str[i] for (int i = 0; i <= n - k; i++) { int dist_count = 0; // To store count of characters // from 'a' to 'z' int[] cnt = new int[26]; Arrays.fill(cnt, 0); // Consider all substrings // between str[i..j] for (int j = i; j < i + k; j++) { // If this is a new vowels // for this substring, // increment dist_count. if (isVowel(str.charAt(j)) && cnt[getIndex(str.charAt(j))] == 0) { dist_count++; } // Increment count of // current character cnt[getIndex(str.charAt(j))]++; } // If count of distinct vowels // in current substring // of length k is less than // equal to x, then increment result. if (dist_count <= x) res++; } return res; } // Driver code public static void main(String args[]) { String s = "TrueGoik"; int K = 3, X = 2; System.out.println(get(s, K, X)); } } // This code is contributed by saurabh_jaiswal.
Python3
# Python code for the above approach # Function to check whether # a character is vowel or not def isVowel(x): return (x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u' or x == 'A' or x == 'E' or x == 'I' or x == 'O' or x == 'U') def getIndex(ch): return (ord(ch) - ord('a')) if (ord(ch) - ord('A')) > 26 else (ord(ch) - ord('A')) # Function to find the count of K length # substring with at most x distinct vowels def get(str, k, x): n = len(str) # Initialize result res = 0 # Consider all substrings # beginning with str[i] for i in range(n - k + 1): dist_count = 0 # To store count of characters # from 'a' to 'z' cnt = [0] * 26 # Consider all substrings # between str[i..j] for j in range(i, i + k): # If this is a new vowels # for this substring, # increment dist_count. if (isVowel(str[j]) and cnt[getIndex(str[j])] == 0): dist_count += 1 # Increment count of # current character cnt[getIndex(str[j])] += 1 # If count of distinct vowels # in current substring # of length k is less than # equal to x, then increment result. if (dist_count <= x): res += 1 return res # Driver code s = "TrueGoik" K = 3 X = 2 print(get(s, K, X)) # This code is contributed by Saurabh Jaiswal
C#
// C# code to implement above approach using System; class GFG { // Function to check whether // a character is vowel or not static bool isVowel(char x) { return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U'); } static int getIndex(char ch) { return (ch - 'A' > 26 ? ch - 'a' : ch - 'A'); } // Function to find the count of K length // substring with at most x distinct vowels static int get(string str, int k, int x) { int n = str.Length; // Initialize result int res = 0; // Consider all substrings // beginning with str[i] for (int i = 0; i <= n - k; i++) { int dist_count = 0; // To store count of characters // from 'a' to 'z' int[] cnt = new int[26]; // Arrays.fill(cnt, 0); // Consider all substrings // between str[i..j] for (int j = i; j < i + k; j++) { // If this is a new vowels // for this substring, // increment dist_count. if (isVowel(str[j]) && cnt[getIndex(str[j])] == 0) { dist_count++; } // Increment count of // current character cnt[getIndex(str[j])]++; } // If count of distinct vowels // in current substring // of length k is less than // equal to x, then increment result. if (dist_count <= x) res++; } return res; } // Driver code public static void Main() { string s = "TrueGoik"; int K = 3, X = 2; Console.WriteLine(get(s, K, X)); } } // This code is contributed by ukasp.
Javascript
<script> // JavaScript code for the above approach // Function to check whether // a character is vowel or not function isVowel(x) { return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U'); } function getIndex(ch) { return ((ch.charCodeAt(0) - 'A'.charCodeAt(0)) > 26 ? (ch.charCodeAt(0) - 'a'.charCodeAt(0)) : (ch.charCodeAt(0) - 'A'.charCodeAt(0))); } // Function to find the count of K length // substring with at most x distinct vowels function get(str, k, x) { let n = str.length; // Initialize result let res = 0; // Consider all substrings // beginning with str[i] for (let i = 0; i <= n - k; i++) { let dist_count = 0; // To store count of characters // from 'a' to 'z' let cnt = new Array(26).fill(0); // Consider all substrings // between str[i..j] for (let j = i; j < i + k; j++) { // If this is a new vowels // for this substring, // increment dist_count. if (isVowel(str[j]) && cnt[getIndex(str[j])] == 0) { dist_count++; } // Increment count of // current character cnt[getIndex(str[j])]++; } // If count of distinct vowels // in current substring // of length k is less than // equal to x, then increment result. if (dist_count <= x) res++; } return res; } // Driver code let s = "TrueGoik"; let K = 3, X = 2; document.write(get(s, K, X)); // This code is contributed by Potta Lokesh </script>
6
Complejidad temporal: O(N * K)
Espacio auxiliar: O(N * K)