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 = 2
Output: 4
Explanation: The substrings are “ae”, “ei”, “io”, “ou”.Input: str = “TrueGeek”, K = 3
Output: 5
Explanation: 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 following
- 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++ 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' );
} // Function to find the count of // substring with k vowels 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 int main( void )
{ string s = "aeiou" ;
int K = 2;
cout << get(s, K);
return 0;
} |
// Java code to implement above approach class 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 approach MAX = 128
# 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' )
# Function to find the count of # substring with k vowels def 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 code if __name__ = = "__main__" :
s = "aeiou"
K = 2
print (get(s, K))
# This code is contributed by rakeshsahni |
// 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' );
}
// 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)