📌  相关文章
📜  计算给定字符串的所有子字符串中出现的元音数 |设置 2

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

计算给定字符串的所有子字符串中出现的元音数 |设置 2

给定一个长度为N的包含0个或多个元音的小写字符的字符串str[] ,任务是找出在给定字符串的所有子字符串中出现的元音计数。

例子:

前缀求和方法:本文的第 1 组讨论了朴素方法和使用前缀求和的方法。

有效方法:假设给定字符串是str ,子字符串从位置x开始,到位置y结束,如果元音在第 i 个位置,其中0<=x<=ii<=y<-N 。对于每个元音,它可以在子字符串中,即 x 的(i+1)个选项和 y(ni)个选项。所以总共有(i+1)*(ni) 个包含str[i] 的子串。取一个常数“ aeiou ”。请按照以下步骤解决问题:

  • 将变量res初始化为0以存储答案。
  • 使用变量i遍历范围[0, N)并执行以下任务:
    • 如果str[i]是元音,则将(I+1)*(Ni)的值添加到变量res以计算可能的元音总数。
  • 执行上述步骤后,打印res的值作为答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of vowels
long long countVowels(string str)
{
 
    // Define the size of the string
    // and ans as zero
    long res = 0, N = str.size();
 
    // Start traversing and find if their
    // is any vowel or not
    for (int i = 0; i < N; ++i)
 
        // If there is vowel, then count
        // the numbers of vowels
        if (string("aeiou").find(str[i])
            != string::npos)
            res += (i + 1) * (N - i);
 
    return res;
}
 
// Driver Code
int main()
{
    string str = "daceh";
 
    // Call the function
    long long ans = countVowels(str);
 
    cout << ans << endl;
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
  // Function to count the number of vowels
  static long countVowels(String str)
  {
 
    // Define the size of the String
    // and ans as zero
    long res = 0, N = str.length();
 
    // Start traversing and find if their
    // is any vowel or not
    for (int i = 0; i < N; ++i)
 
      // If there is vowel, then count
      // the numbers of vowels
      if (new String("aeiou").contains(String.valueOf(str.charAt(i))))
        res += (i + 1) * (N - i);
 
    return res;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String str = "daceh";
 
    // Call the function
    long ans = countVowels(str);
    System.out.print(ans +"\n");
 
  }
}
 
// This code is contributed by shikhasingrajput.


Python3
# Python program for the above approach
 
# Function to count the number of vowels
def countVowels(str):
   
    # Define the size of the String
    # and ans as zero
    res = 0;
    N = len(str);
 
    # Start traversing and find if their
    # is any vowel or not
    for i in range(N):
 
        # If there is vowel, then count
        # the numbers of vowels
        if ((str[i]) in ("aeiou")):
            res += (i + 1) * (N - i);
    return res;
 
# Driver Code
if __name__ == '__main__':
 
    str = "daceh";
 
    # Call the function
    ans = countVowels(str);
    print(ans);
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
class GFG{
 
  // Function to count the number of vowels
  static long countVowels(String str)
  {
 
    // Define the size of the String
    // and ans as zero
    long res = 0, N = str.Length;
 
    // Start traversing and find if their
    // is any vowel or not
    for (int i = 0; i < N; ++i)
 
      // If there is vowel, then count
      // the numbers of vowels
      if (new String("aeiou").Contains(str[i]))
        res += (i + 1) * (N - i);
 
    return res;
  }
 
  // Driver Code
  public static void Main()
  {
    String str = "daceh";
 
    // Call the function
    long ans = countVowels(str);
    Console.Write(ans +"\n");
 
  }
}
 
// This code is contributed by gfgking


Javascript


输出
16

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