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

📅  最后修改于: 2021-06-26 09:20:42             🧑  作者: Mango

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

例子:

简单方法:给定的长度N,可以形成子串的数目= N(N + 1)/ 2的字符串。一个简单的解决方案是针对每个子串,我们计算元音的出现并将其相加以获得结果。该方法的时间复杂度为O(N 3 ),不适用于大的N值。

高效的方法:想法是使用基于前缀和数组的技术,在此技术中,我们将每个字符的出现存储在所有串联的子字符串中。

  • 对于第一个字符,
  • 对于以下每个字符,我们存储

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Returns the total sum of
// occurrences of all vowels
int vowel_calc(string s)
{
    int n = s.length();
    vector arr;
  
    for (int i = 0; i < n; i++) {
  
        if (i == 0)
            // No. of occurrences of 0th character
            // in all the substrings
            arr.push_back(n);
  
        else
            // No. of occurrences of the ith character
            // in all the substrings
            arr.push_back((n - i) + arr[i - 1] - i);
    }
  
    int sum = 0;
    for (int i = 0; i < n; i++)
  
        // Check if ith character is a vowel
        if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
            || s[i] == 'o' || s[i] == 'u')
            sum += arr[i];
  
    // Return the total sum
    // of occurrences of vowels
    return sum;
}
  
// Driver code
int main()
{
    string s = "daceh";
    cout << vowel_calc(s) << endl;
  
    return 0;
}


Java
// Java implementation of the above approach
  
import java.io.*;
import java.util.*;
  
public class Gfg {
  
    // Returns the total sum of
    // occurrences of all vowels
    static int vowel_calc(String s)
    {
        int n = s.length();
        int arr[] = new int[n];
  
        for (int i = 0; i < n; i++) {
  
            if (i == 0)
                // No. of occurrences of 0th character
                // in all the substrings
                arr[i] = n;
  
            else
                // No. of occurrences of ith character
                // in all the substrings
                arr[i] = (n - i) + arr[i - 1] - i;
        }
  
        int sum = 0;
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            // Check if ith character is a vowel
            if (ch == 'a' || ch == 'e' || ch == 'i'
                || ch == 'o' || ch == 'u')
                sum += arr[i];
        }
  
        // Return the total sum
        // of occurrences of vowels
        return sum;
    }
  
    // Driver Code
    public static void main(String args[])
    {
        String s = "daceh";
        System.out.println(vowel_calc(s));
    }
}


Python3
# Python 3 implementation of 
# a more efficient approach.
# return sum of all occurrences of all vowels
def sumVowel(string):
    n = len(string)
    sum = 0
    string = string.lower()
  
    # iterate through every character in the string
    for i in range(0, n):
        s = string[i]
  
        # checks if the character is a vowel or not
        if (s=="a" or s == "e" or s == "i" or s == "o" or s == "u"):
  
            # uses below expression to calculate the count 
                        # of all occurrences of character in substrings 
                        # of the string
            sum += ((n - i) * (i + 1))            
  
    # return the total sum of occurrence
    return sum
  
#driver code
if __name__ == '__main__':
    #input string here
    string = "abhay"
    #print returned sum
    print(sumVowel(string))
  
# This code is contributed by 
# Abhay Subramanian K


C#
// C# implementation of the above approach
   
  
using System;
   
public class Gfg {
   
    // Returns the total sum of
    // occurrences of all vowels
    static int vowel_calc(string s)
    {
        int n = s.Length;
        int[] arr = new int[n];
   
        for (int i = 0; i < n; i++) {
   
            if (i == 0)
                // No. of occurrences of 0th character
                // in all the substrings
                arr[i] = n;
   
            else
                // No. of occurrences of ith character
                // in all the substrings
                arr[i] = (n - i) + arr[i - 1] - i;
        }
   
        int sum = 0;
        for (int i = 0; i < n; i++) {
            char ch = s[i];
            // Check if ith character is a vowel
            if (ch == 'a' || ch == 'e' || ch == 'i'
                || ch == 'o' || ch == 'u')
                sum += arr[i];
        }
   
        // Return the total sum
        // of occurrences of vowels
        return sum;
    }
   
    // Driver Code
    public static void Main()
    {
        string s = "daceh";
        Console.Write(vowel_calc(s));
    }
}


PHP


输出:
16

时间复杂度: O(N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。