📌  相关文章
📜  包含恰好 K 个不同元音的子串计数

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

包含恰好 K 个不同元音的子串计数

给定大小为N的字符串str ,包含大写和小写字母,以及一个整数K 。任务是找出恰好包含K个不同元音的子串的数量。

例子:

方法:可以通过生成所有子字符串来解决问题。从生成的子串中计算具有 K 个不同元音的子串。按照下面提到的步骤来实施该方法:

  • 首先生成从[0, N]范围内的每个索引i开始的所有子字符串
  • 然后对于每个子字符串,请按照以下步骤操作:
    • 保留一个哈希数组来存储唯一元音的出现。
    • 检查子字符串中的新字符是否为元音
    • 如果它是元音,则增加其在哈希中的出现次数并保留找到的不同元音的计数
    • 现在对于每个子字符串,如果元音的不同计数是K ,则增加最终计数
  • 如果对于任何循环查找从i开始的子串,不同元音的计数超过 K ,或者,子串长度已达到字符串长度,则中断循环并查找从i+1开始的子串。
  • 考虑完所有子字符串后,打印最终计数。

下面是上述方法的实现。

C++
// C++ program to count number of 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 count number of substrings
// with exactly k unique vowels
int countkDist(string str, int k)
{
    int n = str.length();
  
    // Initialize result
    int res = 0;
  
    // 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 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 distinct vowels count
            // becomes k then increment result
            if (dist_count == k) 
                res++;
  
            if (dist_count > k)
                break;
        }
    }
    return res;
}
  
// Driver code
int main()
{
    string str = "TrueGoik";
    int K = 3;
    cout << countkDist(str, K) << endl;
    return 0;
}


Java
// Java program to count number of substrings
// with exactly k distinct vowels
import java.util.*;
public 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');
}
  
static int getIndex(char ch)
{
    return (ch - 'A' > 26 ? ch - 'a' : 
            ch - 'A');
}
  
// Function to count number of substrings
// with exactly k unique vowels
static int countkDist(String str, int k)
{
    int n = str.length();
  
    // Initialize result
    int res = 0;
  
    // 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];
        for(int t = 0; t < 26; t++) {
            cnt[t] = 0;
        }
          
        // Consider all substrings 
        // between str[i..j]
        for (int j = i; j < n; 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 distinct vowels count
            // becomes k then increment result
            if (dist_count == k) 
                res++;
  
            if (dist_count > k)
                break;
        }
    }
    return res;
}
  
// Driver code
public static void main(String args[])
{
    String str = "TrueGoik";
    int K = 3;
    System.out.println(countkDist(str, K));
}
}
  
// This code is contributed by Samim Hossain Mondal.


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 count number of substrings
# with exactly k unique vowels
def countkDist(str, k):
    n = len(str)
  
    # Initialize result
    res = 0
  
    # Consider all substrings
    # beginning with str[i]
    for i in range(n):
        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, n):
  
            # 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 distinct vowels count
            # becomes k then increment result
            if (dist_count == k):
                res += 1
  
            if (dist_count > k):
                break
    return res
  
# Driver code
s = "TrueGoik"
K = 3
  
print(countkDist(s, K))
  
# This code is contributed by Saurabh Jaiswal


C#
// C# program to count number of 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 count number of substrings
  // with exactly k unique vowels
  static int countkDist(string str, int k)
  {
    int n = str.Length;
  
    // Initialize result
    int res = 0;
  
    // 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];
      for(int t = 0; t < 26; t++) {
        cnt[t] = 0;
      }
  
      // Consider all substrings 
      // between str[i..j]
      for (int j = i; j < n; 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 distinct vowels count
        // becomes k then increment result
        if (dist_count == k) 
          res++;
  
        if (dist_count > k)
          break;
      }
    }
    return res;
  }
  
  // Driver code
  public static void Main()
  {
    string str = "TrueGoik";
    int K = 3;
    Console.Write(countkDist(str, K));
  }
}
  
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
5

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