查找包含恰好 K 个唯一元音的所有子串
给定长度为N的字符串str包含大写和小写字母,以及一个整数K 。任务是找到包含恰好K个不同元音的所有子串。
例子:
Input: str = “aeiou”, K = 2
Output: “ae”, “ei”, “io”, “ou”
Explanation: These are the substrings containing exactly 2 distinct vowels.
Input: str = “TrueGeek”, K = 3
Output: “”
Explanation: Though the string has more than 3 vowels but there is not three unique vowels.
So the answer is empty.
Input: str = “TrueGoik”, K = 3
Output: “TrueGo”, “rueGo”, “ueGo”, “eGoi”, “eGoik”
方法:这个问题可以通过贪心方法来解决。生成子字符串并检查每个子字符串。请按照以下步骤解决问题:
- 首先生成从0到N范围内的每个索引i开始的所有子字符串。
- 然后对于每个子字符串:
- 保留一个哈希数组来存储唯一元音的出现。
- 检查子字符串中的新字符是否为元音。
- 如果它是元音,则增加其在哈希中的出现次数并保留找到的不同元音的计数
- 现在对于每个子字符串,如果元音的不同计数是K ,则打印子字符串。
- 如果对于任何循环查找从i开始的子串,不同元音的计数超过K ,或者,子串长度已达到字符串长度,则中断循环并查找从i+1开始的子串。
下面是上述方法的实现。
C++
// C++ program to find all substrings with
// exactly k distinct vowels
#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');
}
int getIndex(char ch)
{
return (ch - 'A' > 26 ?
ch - 'a' : ch - 'A');
}
// Function to find all substrings
// with exactly k unique vowels
void countkDist(string str, int k)
{
int n = str.length();
// Consider all substrings
// beginning with str[i]
for (int i = 0; i < n; i++) {
int dist_count = 0;
// To store count of characters
// from 'a' to 'z'
vector cnt(26, 0);
// Consider all substrings
// between str[i..j]
for (int j = i; j < n; j++) {
// If this is a new vowel
// 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 distinct vowels count
// becomes k, then print the
// substring.
if (dist_count == k) {
cout << str.substr(i, j - i + 1) << endl;
}
if (dist_count > k)
break;
}
}
}
// Driver code
int main()
{
string str = "TrueGoik";
int K = 3;
countkDist(str, K);
return 0;
}
Java
// Java program to find all subStrings with
// exactly k distinct vowels
import java.util.*;
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');
}
static int getIndex(char ch)
{
return (ch - 'A' > 26 ?
ch - 'a' : ch - 'A');
}
// Function to find all subStrings
// with exactly k unique vowels
static void countkDist(String str, int k)
{
int n = str.length();
// Consider all subStrings
// beginning with str[i]
for (int i = 0; i < n; i++) {
int dist_count = 0;
// To store count of characters
// from 'a' to 'z'
int[] cnt = new int[26];
// Consider all subStrings
// between str[i..j]
for (int j = i; j < n; j++) {
String print = new String(str);
// If this is a new vowel
// 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 distinct vowels count
// becomes k, then print the
// subString.
if (dist_count == k) {
System.out.print(print.substring(i, j +1) +"\n");
}
if (dist_count > k)
break;
}
}
}
// Driver code
public static void main(String[] args)
{
String str = "TrueGoik";
int K = 3;
countkDist(str, K);
}
}
// This code is contributed by Rajput-Ji
Python3
# python3 program to find all substrings with
# exactly k distinct vowels
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')
def getIndex(ch):
if ord(ch) - ord('A') > 26:
return ord(ch) - ord('a')
else:
return ord(ch) - ord('A')
# Function to find all substrings
# with exactly k unique vowels
def countkDist(str, k):
n = len(str)
# Consider all substrings
# beginning with str[i]
for i in range(0, n):
dist_count = 0
# To store count of characters
# from 'a' to 'z'
cnt = [0 for _ in range(26)]
# Consider all substrings
# between str[i..j]
for j in range(i, n):
# If this is a new vowel
# 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 distinct vowels count
# becomes k, then print the
# substring.
if (dist_count == k):
print(str[i:i+j - i + 1])
if (dist_count > k):
break
# Driver code
if __name__ == "__main__":
str = "TrueGoik"
K = 3
countkDist(str, K)
# This code is contributed by rakeshsahni
C#
// C# program to find all substrings with
// exactly k distinct vowels
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 all substrings
// with exactly k unique vowels
static void countkDist(string str, int k)
{
int n = str.Length;
// Consider all substrings
// beginning with str[i]
for (int i = 0; i < n; i++) {
int dist_count = 0;
// To store count of characters
// from 'a' to 'z'
int[] cnt = new int[26];
// Consider all substrings
// between str[i..j]
for (int j = i; j < n; j++) {
// If this is a new vowel
// 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 distinct vowels count
// becomes k, then print the
// substring.
if (dist_count == k) {
Console.WriteLine(
str.Substring(i, j - i + 1));
}
if (dist_count > k)
break;
}
}
}
// Driver code
public static void Main()
{
string str = "TrueGoik";
int K = 3;
countkDist(str, K);
}
}
// This code is contributed by ukasp.
Javascript
输出
TrueGo
rueGo
ueGo
eGoi
eGoik
时间复杂度: O(N 2 )
辅助空间: O(N 2 ) 存储结果子串