恰好包含 K 个元音的子串计数
给定字符串str包含大写和小写字母,以及一个整数K 。任务是找出恰好包含 K 个元音(可能是重复的)的子串的数量。
例子:
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”.
方法:解决这个问题是基于贪婪的方法。生成所有子字符串并为每个子字符串检查元音的数量。请按照以下步骤操作。
- 生成所有子字符串。对于每个子字符串,请执行以下操作
- 存储元音的出现次数。
- 检查子字符串中的新字符是否为元音。
- 如果是元音,则增加找到的元音计数
- 现在对于每个子字符串,如果元音的计数是 K ,则增加最终计数。
- 最后返回最终计数作为所需答案。
下面是上述代码的实现。
C++
// C++ code to implement above approach
#include
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
// 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
Python3
# 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#
// 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.
Javascript
输出
4
时间复杂度: O(N 2 ) 其中 N 是字符串的长度。
辅助空间: O(1)