给定一个长度为 N 的包含 0 个或多个元音的小写字符的字符串,任务是找到在给定字符串 的所有子字符串中出现的元音数。
例子:
Input: str = “abc”
Output: 3
The given string “abc” contains only one vowel = ‘a’
Substrings of “abc” are = {“a”, “b”, “c”, “ab”, “bc, “abc”}
Hence, the sum of occurrences of the vowel in these strings = 3.(‘a’ occurred 3 times)
Input: str = “daceh”
Output: 16
简单方法:给定的长度N,可以形成子串的数目= N(N + 1)/ 2的字符串。一个简单的解决方案是对于每个子字符串,我们计算元音的出现次数并将它们相加以获得结果。这种方法的时间复杂度为 O(N 3 ),不适用于较大的 N 值。
有效的方法:这个想法是使用基于前缀和数组的技术,我们将每个字符在所有连接的子字符串中的出现次数存储起来。
- 对于第一个字符,
no. of occurrences = no. of substrings starting with the first character = N.
- 对于以下每个字符,我们存储
no. of substrings starting with that character + the number of substrings formed by the previous characters containing this character – the number of substrings formed by the previous characters only.
下面是上述方法的实现:
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
Javascript
16
时间复杂度: O(N)