📜  包含最多 X 个不同元音的 K 个长度子串的计数

📅  最后修改于: 2022-05-13 01:56:08.994000             🧑  作者: Mango

包含最多 X 个不同元音的 K 个长度子串的计数

给定大小为N的字符串str ,包含大写和小写字母,以及两个整数KX 。任务是找到包含最多 X个不同元音的大小为K的子串的计数。

例子:

方法:要解决这个问题,首先必须生成所有长度为K的子串。然后在每个子字符串中检查不同元音的数量是否小于X。请按照以下步骤操作。

  • 首先从[0, N – K]中的每个索引i开始生成所有长度为K的子串。
  • 然后对于每个长度为K的子字符串,执行以下操作:
    • 保留一个散列来存储唯一元音的出现。
    • 检查子字符串中的新字符是否为元音
    • 如果它是元音,则增加其在哈希中的出现次数并保留找到的不同元音的计数
    • 现在对于每个子字符串,如果元音的不同计数小于或等于X ,则增加最终计数。
  • 考虑完所有子字符串后,打印最终计数。

下面是上述方法的实现:

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');
}
 
int getIndex(char ch)
{
    return (ch - 'A' > 26 ? ch - 'a' :
            ch - 'A');
}
 
// Function to find the count of K length
// substring with at most x distinct vowels
int get(string str, int k, int x)
{
    int n = str.length();
 
    // Initialize result
    int res = 0;
 
    // Consider all substrings
    // beginning with str[i]
    for (int i = 0; i <= n - k; 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 < i + k; j++) {
 
            // If this is a new vowels
            // 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 count of distinct vowels
        // in current substring
        // of length k is less than
        // equal to x, then increment result.
        if (dist_count <= x)
            res++;
    }
 
    return res;
}
 
// Driver code
int main(void)
{
    string s = "TrueGoik";
    int K = 3, X = 2;
    cout << get(s, K, X);
    return 0;
}


Java
// Java code to implement above approach
import java.util.Arrays;
 
class GFG {
  static 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 the count of K length
  // substring with at most x distinct vowels
  static int get(String str, int k, int x) {
    int n = str.length();
 
    // Initialize result
    int res = 0;
 
    // Consider all substrings
    // beginning with str[i]
    for (int i = 0; i <= n - k; i++) {
      int dist_count = 0;
 
      // To store count of characters
      // from 'a' to 'z'
      int[] cnt = new int[26];
      Arrays.fill(cnt, 0);
 
      // Consider all substrings
      // between str[i..j]
      for (int j = i; j < i + k; j++) {
 
        // If this is a new vowels
        // 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 count of distinct vowels
      // in current substring
      // of length k is less than
      // equal to x, then increment result.
      if (dist_count <= x)
        res++;
    }
 
    return res;
  }
 
  // Driver code
  public static void main(String args[]) {
    String s = "TrueGoik";
    int K = 3, X = 2;
    System.out.println(get(s, K, X));
  }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Python code for the above approach
 
# 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):
    return (ord(ch) - ord('a')) if (ord(ch) - ord('A')) > 26 else (ord(ch) - ord('A'))
 
# Function to find the count of K length
# substring with at most x distinct vowels
def get(str, k, x):
    n = len(str)
 
    # Initialize result
    res = 0
 
    # Consider all substrings
    # beginning with str[i]
    for i in range(n - k + 1):
        dist_count = 0
 
        # To store count of characters
        # from 'a' to 'z'
        cnt = [0] * 26
 
        # Consider all substrings
        # between str[i..j]
        for j in range(i, i + k):
 
            # If this is a new vowels
            # 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 count of distinct vowels
        # in current substring
        # of length k is less than
        # equal to x, then increment result.
        if (dist_count <= x):
            res += 1
 
    return res
 
# Driver code
 
s = "TrueGoik"
K = 3
X = 2
print(get(s, K, X))
 
# This code is contributed by Saurabh Jaiswal


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');
  }
 
  static int getIndex(char ch)
  {
    return (ch - 'A' > 26 ? ch - 'a' : ch - 'A');
  }
 
  // Function to find the count of K length
  // substring with at most x distinct vowels
  static int get(string str, int k, int x)
  {
    int n = str.Length;
 
    // Initialize result
    int res = 0;
 
    // Consider all substrings
    // beginning with str[i]
    for (int i = 0; i <= n - k; i++) {
      int dist_count = 0;
 
      // To store count of characters
      // from 'a' to 'z'
      int[] cnt = new int[26];
      // Arrays.fill(cnt, 0);
 
      // Consider all substrings
      // between str[i..j]
      for (int j = i; j < i + k; j++) {
 
        // If this is a new vowels
        // 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 count of distinct vowels
      // in current substring
      // of length k is less than
      // equal to x, then increment result.
      if (dist_count <= x)
        res++;
    }
 
    return res;
  }
 
  // Driver code
  public static void Main()
  {
    string s = "TrueGoik";
    int K = 3, X = 2;
    Console.WriteLine(get(s, K, X));
  }
}
 
// This code is contributed by ukasp.


Javascript



输出
6

时间复杂度: O(N * K)
辅助空间: O(N * K)