Count of substrings containing exactly K vowels - GeeksforGeeks

29 Dec, 2021
Given string str containing both uppercase and lowercase letters, and an integer K. The task is to find the count of substrings containing exactly K vowels (maybe repetitive).
Examples:
Input: str = “aeiou”, K = 2Output: 4Explanation: The substrings are “ae”, “ei”, “io”, “ou”.
Input: str = “TrueGeek”, K = 3Output: 5Explanation: All such possible substrings are:“TrueGe”, “rueGe”, “ueGe”, “eGee”, “eGeek”.
Approach: To solution of this problem is based on greedy approach. Generate all substrings and for each substring check the count of vowels. Follow the steps mentioned below.
Generate all the substrings. For each substring do the followingStore the count of occurrences of vowels.Check if a new character in the substring is a vowel or not.If it is a vowel, increment count of vowels foundNow for each substring, if the count of vowels is K, increment the final count.
Store the count of occurrences of vowels.
Check if a new character in the substring is a vowel or not.
If it is a vowel, increment count of vowels found
Now for each substring, if the count of vowels is K, increment the final count.
Return the final count as the required answer at the end.
Below is the implementation of the above code.
C++
Java
Python3
C#
Javascript
// 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 notbool isVowel(char x){    return (x == 'a' || x == 'e' ||             x == 'i' || x == 'o' || x == 'u'             || x == 'A' || x == 'E' ||             x == 'I' || x == 'O' || x == 'U');}  // Function to find the count of// substring with k vowelsint get(string str, int k){      int n = str.length();      // Stores the count of    // substring with K vowels    int ans = 0;      // Consider all substrings     // beginning with str[i]    for (int i = 0; i < n; i++) {        int count = 0;          // Consider all substrings         // between [i, j]        for (int j = i; j < n; j++) {              // If this is a vowel, for this            // substring, increment count.            if (isVowel(str[j])) {                count++;            }              // If vowel count becomes k,            // then increment final count.            if (count == k) {                ans++;            }              if (count > k)                break;        }    }    return ans;}  // Driver codeint main(void){    string s = "aeiou";    int K = 2;    cout << get(s, K);    return 0;}
// Java code to implement above approachclass GFG{    static final 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');  }    // Function to find the count of  // subString with k vowels  static int get(String str, int k)  {      int n = str.length();      // Stores the count of    // subString with K vowels    int ans = 0;      // Consider all subStrings     // beginning with str[i]    for (int i = 0; i < n; i++) {      int count = 0;        // Consider all subStrings       // between [i, j]      for (int j = i; j < n; j++) {          // If this is a vowel, for this        // subString, increment count.        if (isVowel(str.charAt(j))) {          count++;        }          // If vowel count becomes k,        // then increment final count.        if (count == k) {          ans++;        }          if (count > k)          break;      }    }    return ans;  }    // Driver code  public static void main(String[] args)  {    String s = "aeiou";    int K = 2;    System.out.print(get(s, K));  }}  // This code is contributed by 29AjayKumar
# python code to implement above approachMAX = 128  # Function to check whether# a character is vowel or notdef 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')  # Function to find the count of# substring with k vowelsdef get(str, k):      n = len(str)      # Stores the count of    # substring with K vowels    ans = 0      # Consider all substrings    # beginning with str[i]    for i in range(0, n):        count = 0          # Consider all substrings        # between [i, j]        for j in range(i, n):              # If this is a vowel, for this            # substring, increment count.            if (isVowel(str[j])):                count += 1              # If vowel count becomes k,            # then increment final count.            if (count == k):                ans += 1              if (count > k):                break      return ans  # Driver codeif __name__ == "__main__":      s = "aeiou"    K = 2    print(get(s, K))  # This code is contributed by rakeshsahni
// C# code to implement above approachusing 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');  }    // Function to find the count of  // substring with k vowels  static int get(string str, int k)  {      int n = str.Length;      // Stores the count of    // substring with K vowels    int ans = 0;      // Consider all substrings    // beginning with str[i]    for (int i = 0; i < n; i++) {      int count = 0;        // Consider all substrings      // between [i, j]      for (int j = i; j < n; j++) {          // If this is a vowel, for this        // substring, increment count.        if (isVowel(str[j])) {          count++;        }          // If vowel count becomes k,        // then increment final count.        if (count == k) {          ans++;        }          if (count > k)          break;      }    }    return ans;  }    // Driver code  public static void Main()  {    string s = "aeiou";    int K = 2;    Console.WriteLine(get(s, K));  }}  // This code is contributed by ukasp.
<script>   // JavaScript code for the above approach    let MAX = 128    // 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 to find the count of   // substring with k vowels   function get(str, k) {      let n = str.length;      // Stores the count of     // substring with K vowels     let ans = 0;      // Consider all substrings      // beginning with str[i]     for (let i = 0; i < n; i++) {       let count = 0;        // Consider all substrings        // between [i, j]       for (let j = i; j < n; j++) {          // If this is a vowel, for this         // substring, increment count.         if (isVowel(str[j])) {           count++;         }          // If vowel count becomes k,         // then increment final count.         if (count == k) {           ans++;         }          if (count > k)           break;       }     }     return ans;   }    // Driver code   let s = "aeiou";   let K = 2;   document.write(get(s, K));  // This code is contributed by Potta Lokesh </script>
  
4
 
Time Complexity: O(N2) where N is the length of the string.Auxiliary Space: O(1)
 
lokeshpotta20
rakeshsahni
ukasp
29AjayKumar
Algo-Geek 2021
substring
Algo Geek
Greedy
Strings
Strings
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check if the given string is valid English word or not
Sort strings on the basis of their numeric part
Divide given number into two even parts
Bit Manipulation technique to replace boolean arrays of fixed size less than 64
Count of Palindrome Strings in given Array of strings
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Program for array rotation
Write a program to print all permutations of a given string