📌  相关文章
📜  包含恰好 X 个元音的 K 个长度子串的计数

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

包含恰好 X 个元音的 K 个长度子串的计数

给定包含大写和小写英文字母的N个字符的字符串str ,任务是找到大小为K的子字符串正好包含X个元音的计数。

例子:

方法:给定的问题可以使用滑动窗口技术来解决,方法是维护一个大小为K的窗口并跟踪当前窗口中遇到的元音数量。如果当前窗口中的元音计数等于X ,则将最终所需的计数增加1

下面是上述方法的实现:

C++
// C++ program of the 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
// K-sized substring having X vowels
int cntSubstr(string str, int K, int X)
{
    // Stores the number of vowels
    // in the current window
    int vow = 0;
 
    for (int i = 0; i < K; i++)
        if (isVowel(str[i]))
            vow++;
 
    // Stores the count of K length
    // substring with X vowels
    int ans = vow == X ? 1 : 0;
 
    for (int i = 1; i < str.length(); i++) {
 
        // Remove (i - 1)th character
        // from the current window
        vow = isVowel(str[i - 1]) ? vow - 1 : vow;
 
        // Insert (i - 1 + K)th character
        // from the current window
        vow = isVowel(str[i - 1 + K]) ? vow + 1 : vow;
 
        if (vow == X)
 
            // Increment answer
            ans++;
    }
 
    // Return Answer
    return ans;
}
 
// Driver code
int main(void)
{
    string s = "TrueGeek";
    int K = 3, X = 2;
 
    cout << cntSubstr(s, K, X);
 
    return 0;
}


Java
// Java code to implement the above approach
import java.io.*;
class GFG
{
 
  // 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
  // K-sized subString having X vowels
  static int cntSubstr(String str, int K, int X)
  {
 
    // Stores the number of vowels
    // in the current window
    int vow = 0;
 
    for (int i = 0; i < K; i++)
      if (isVowel(str.charAt(i)))
        vow++;
 
    // Stores the count of K length()
    // subString with X vowels
    int ans = vow == X ? 1 : 0;
 
    for (int i = 1; i < str.length(); i++) {
 
      // Remove (i - 1)th character
      // from the current window
      vow = isVowel(str.charAt(i - 1)) ? vow - 1 : vow;
 
      // Insert (i - 1 + K)th character
      // from the current window
      if(i - 1 +  K < str.length())
        vow = isVowel(str.charAt(i - 1 + K)) ? vow + 1 : vow;
 
      if (vow == X)
 
        // Increment answer
        ans++;
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver code
  public static void main (String[] args) {
    String s = "TrueGeek";
    int K = 3, X = 2;
 
    System.out.println(cntSubstr(s, K, X));
  }
}
 
// This code is contributed by Shubham Singh


Python3
# Python3 program of the 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
# K-sized substring having X vowels
def cntSubstr(str, K, X):
 
    # Stores the number of vowels
    # in the current window
    vow = 0
 
    for i in range(0, K):
        if (isVowel(str[i])):
            vow += 1
 
    # Stores the count of K length
    # substring with X vowels
    ans = 1 if vow == X else 0
 
    for i in range(1, len(str) - K + 1):
 
        # Remove (i - 1)th character
        # from the current window
        vow = vow - 1 if isVowel(str[i - 1]) else vow
 
        # Insert (i - 1 + K)th character
        # from the current window
        vow = vow + 1 if isVowel(str[i - 1 + K]) else vow
 
        if (vow == X):
 
            # Increment answer
            ans += 1
 
    # Return Answer
    return ans
 
# Driver code
if __name__ == "__main__":
 
    s = "TrueGeek"
    K, X = 3, 2
 
    print(cntSubstr(s, K, X))
 
# This code is contributed by rakeshsahni


C#
// C# code to implement the 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
  // K-sized substring having X vowels
  static int cntSubstr(string str, int K, int X)
  {
    // Stores the number of vowels
    // in the current window
    int vow = 0;
 
    for (int i = 0; i < K; i++)
      if (isVowel(str[i]))
        vow++;
 
    // Stores the count of K length
    // substring with X vowels
    int ans = vow == X ? 1 : 0;
 
    for (int i = 1; i < str.Length; i++) {
 
      // Remove (i - 1)th character
      // from the current window
      vow = isVowel(str[i - 1]) ? vow - 1 : vow;
 
      // Insert (i - 1 + K)th character
      // from the current window
      if(i - 1 +  K < str.Length)
        vow = isVowel(str[i - 1 + K]) ? vow + 1 : vow;
 
      if (vow == X)
 
        // Increment answer
        ans++;
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver code
  public static void Main()
  {
    string s = "TrueGeek";
    int K = 3, X = 2;
 
    Console.Write(cntSubstr(s, K, X));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
5

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