Count of substrings containing exactly K vowels
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++
#include <bits/stdc++.h>
using namespace std;
#define MAX 128
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 get(string str, int k)
{
int n = str.length();
int ans = 0;
for ( int i = 0; i < n; i++) {
int count = 0;
for ( int j = i; j < n; j++) {
if (isVowel(str[j])) {
count++;
}
if (count == k) {
ans++;
}
if (count > k)
break ;
}
}
return ans;
}
int main( void )
{
string s = "aeiou" ;
int K = 2;
cout << get(s, K);
return 0;
}
|
Java
class GFG
{
static final int MAX = 128 ;
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 get(String str, int k)
{
int n = str.length();
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
int count = 0 ;
for ( int j = i; j < n; j++) {
if (isVowel(str.charAt(j))) {
count++;
}
if (count == k) {
ans++;
}
if (count > k)
break ;
}
}
return ans;
}
public static void main(String[] args)
{
String s = "aeiou" ;
int K = 2 ;
System.out.print(get(s, K));
}
}
|
Python3
MAX = 128
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 get( str , k):
n = len ( str )
ans = 0
for i in range ( 0 , n):
count = 0
for j in range (i, n):
if (isVowel( str [j])):
count + = 1
if (count = = k):
ans + = 1
if (count > k):
break
return ans
if __name__ = = "__main__" :
s = "aeiou"
K = 2
print (get(s, K))
|
C#
using System;
class GFG {
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 get ( string str, int k)
{
int n = str.Length;
int ans = 0;
for ( int i = 0; i < n; i++) {
int count = 0;
for ( int j = i; j < n; j++) {
if (isVowel(str[j])) {
count++;
}
if (count == k) {
ans++;
}
if (count > k)
break ;
}
}
return ans;
}
public static void Main()
{
string s = "aeiou" ;
int K = 2;
Console.WriteLine( get (s, K));
}
}
|
Javascript
<script>
let MAX = 128
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 get(str, k) {
let n = str.length;
let ans = 0;
for (let i = 0; i < n; i++) {
let count = 0;
for (let j = i; j < n; j++) {
if (isVowel(str[j])) {
count++;
}
if (count == k) {
ans++;
}
if (count > k)
break ;
}
}
return ans;
}
let s = "aeiou" ;
let K = 2;
document.write(get(s, K));
</script>
|
Time Complexity: O(N2) where N is the length of the string.
Auxiliary Space: O(1)
Last Updated :
29 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...