📌  相关文章
📜  计算包含所有元音的子字符串|设置2

📅  最后修改于: 2021-05-30 13:49:03             🧑  作者: Mango

给定一个包含小写字母的字符串str ,任务是至少计算一次包含所有元音的子字符串,并且子字符串中不存在辅音(非元音字符)。

例子:

方法:想法是提取仅包含元音的所有最大长度子字符串。现在,对于所有这些子字符串,我们需要至少找到一次包含所有元音的子字符串的计数。这可以使用两指针技术来完成。

在这种情况下如何使用两指针技术的说明:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if c is a vowel
bool isVowel(char c)
{
    return (c == 'a' || c == 'e' || c == 'i'
            || c == 'o' || c == 'u');
}
  
// Function to return the count of sub-strings
// that contain every vowel at least
// once and no consonant
int countSubstringsUtil(string s)
{
    int count = 0;
  
    // Map is used to store count of each vowel
    map mp;
  
    int n = s.length();
  
    // Start index is set to 0 initially
    int start = 0;
  
    for (int i = 0; i < n; i++) {
        mp[s[i]]++;
  
        // If substring till now have all vowels
        // atleast once increment start index until
        // there are all vowels present between
        // (start, i) and add n - i each time
        while (mp['a'] > 0 && mp['e'] > 0
               && mp['i'] > 0 && mp['o'] > 0
               && mp['u'] > 0) {
            count += n - i;
            mp[s[start]]--;
            start++;
        }
    }
  
    return count;
}
  
// Function to extract all maximum length
// sub-strings in s that contain only vowels
// and then calls the countSubstringsUtil() to find
// the count of valid sub-strings in that string
int countSubstrings(string s)
{
    int count = 0;
    string temp = "";
  
    for (int i = 0; i < s.length(); i++) {
  
        // If current character is a vowel then
        // append it to the temp string
        if (isVowel(s[i])) {
            temp += s[i];
        }
  
        // The sub-string containing all vowels ends here
        else {
  
            // If there was a valid sub-string
            if (temp.length() > 0)
                count += countSubstringsUtil(temp);
  
            // Reset temp string
            temp = "";
        }
    }
  
    // For the last valid sub-string
    if (temp.length() > 0)
        count += countSubstringsUtil(temp);
  
    return count;
}
  
// Driver code
int main()
{
    string s = "aeouisddaaeeiouua";
  
    cout << countSubstrings(s) << endl;
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function that returns true if c is a vowel
static boolean isVowel(char c)
{
    return (c == 'a' || c == 'e' || 
            c == 'i' || c == 'o' || c == 'u');
}
  
// Function to return the count of sub-strings
// that contain every vowel at least
// once and no consonant
static int countSubstringsUtil(char []s)
{
    int count = 0;
  
    // Map is used to store count of each vowel
    Map mp = new HashMap<>();
  
    int n = s.length;
  
    // Start index is set to 0 initially
    int start = 0;
  
    for (int i = 0; i < n; i++) 
    {
        if(mp.containsKey(s[i]))
        {
            mp.put(s[i], mp.get(s[i]) + 1);
        }
        else
        {
            mp.put(s[i], 1);
        }
  
        // If substring till now have all vowels
        // atleast once increment start index until
        // there are all vowels present between
        // (start, i) and add n - i each time
        while (mp.containsKey('a') && mp.containsKey('e') &&
               mp.containsKey('i') && mp.containsKey('o') &&
               mp.containsKey('u') && mp.get('a') > 0 && 
               mp.get('e') > 0 && mp.get('i') > 0 && 
               mp.get('o') > 0 && mp.get('u') > 0) 
        {
            count += n - i;
            mp.put(s[start], mp.get(s[start]) - 1);
  
            start++;
        }
    }
    return count;
}
  
// Function to extract all maximum length
// sub-strings in s that contain only vowels
// and then calls the countSubstringsUtil() to find
// the count of valid sub-strings in that string
static int countSubstrings(String s)
{
    int count = 0;
    String temp = "";
  
    for (int i = 0; i < s.length(); i++) 
    {
  
        // If current character is a vowel then
        // append it to the temp string
        if (isVowel(s.charAt(i))) 
        {
            temp += s.charAt(i);
        }
  
        // The sub-string containing all vowels ends here
        else
        {
  
            // If there was a valid sub-string
            if (temp.length() > 0)
                count += countSubstringsUtil(temp.toCharArray());
  
            // Reset temp string
            temp = "";
        }
    }
  
    // For the last valid sub-string
    if (temp.length() > 0)
        count += countSubstringsUtil(temp.toCharArray());
  
    return count;
}
  
// Driver code
public static void main(String[] args) 
{
    String s = "aeouisddaaeeiouua";
  
    System.out.println(countSubstrings(s));
}
} 
  
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the approach 
  
# Function that returns true if c is a vowel 
def isVowel(c) : 
  
    return (c == 'a' or c == 'e' or c == 'i'
            or c == 'o' or c == 'u'); 
  
  
# Function to return the count of sub-strings 
# that contain every vowel at least 
# once and no consonant 
def countSubstringsUtil(s) : 
  
    count = 0; 
  
    # Map is used to store count of each vowel 
    mp = dict.fromkeys(s,0); 
  
    n = len(s); 
  
    # Start index is set to 0 initially 
    start = 0; 
  
    for i in range(n) :
        mp[s[i]] += 1; 
  
        # If substring till now have all vowels 
        # atleast once increment start index until 
        # there are all vowels present between 
        # (start, i) and add n - i each time 
        while (mp['a'] > 0 and mp['e'] > 0
            and mp['i'] > 0 and mp['o'] > 0
            and mp['u'] > 0) :
            count += n - i; 
            mp[s[start]] -= 1; 
            start += 1; 
  
    return count; 
  
# Function to extract all maximum length 
# sub-strings in s that contain only vowels 
# and then calls the countSubstringsUtil() to find 
# the count of valid sub-strings in that string 
def countSubstrings(s) : 
  
    count = 0; 
    temp = ""; 
  
    for i in range(len(s)) :
  
        # If current character is a vowel then 
        # append it to the temp string 
        if (isVowel(s[i])) :
            temp += s[i]; 
  
        # The sub-string containing all vowels ends here 
        else :
  
            # If there was a valid sub-string 
            if (len(temp) > 0) :
                count += countSubstringsUtil(temp); 
  
            # Reset temp string 
            temp = ""; 
  
    # For the last valid sub-string 
    if (len(temp) > 0) :
        count += countSubstringsUtil(temp); 
  
    return count; 
  
# Driver code 
if __name__ == "__main__" : 
  
    s = "aeouisddaaeeiouua"; 
  
    print(countSubstrings(s)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG 
{
  
// Function that returns true if c is a vowel
static bool isVowel(char c)
{
    return (c == 'a' || c == 'e' || 
            c == 'i' || c == 'o' || c == 'u');
}
  
// Function to return the count of sub-strings
// that contain every vowel at least
// once and no consonant
static int countSubstringsUtil(char []s)
{
    int count = 0;
  
    // Map is used to store count of each vowel
    Dictionary mp = new Dictionary();
  
    int n = s.Length;
  
    // Start index is set to 0 initially
    int start = 0;
  
    for (int i = 0; i < n; i++) 
    {
        if(mp.ContainsKey(s[i]))
        {
            mp[s[i]] = mp[s[i]] + 1;
        }
        else
        {
            mp.Add(s[i], 1);
        }
  
        // If substring till now have all vowels
        // atleast once increment start index until
        // there are all vowels present between
        // (start, i) and add n - i each time
        while (mp.ContainsKey('a') && mp.ContainsKey('e') &&
               mp.ContainsKey('i') && mp.ContainsKey('o') &&
               mp.ContainsKey('u') && mp['a'] > 0 && 
               mp['e'] > 0 && mp['i'] > 0 && 
               mp['o'] > 0 && mp['u'] > 0) 
        {
            count += n - i;
            if(mp.ContainsKey(s[start]))
                mp[s[start]] = mp[s[start]] - 1;
  
            start++;
        }
    }
    return count;
}
  
// Function to extract all maximum length
// sub-strings in s that contain only vowels
// and then calls the countSubstringsUtil() to find
// the count of valid sub-strings in that string
static int countSubstrings(String s)
{
    int count = 0;
    String temp = "";
  
    for (int i = 0; i < s.Length; i++) 
    {
  
        // If current character is a vowel then
        // append it to the temp string
        if (isVowel(s[i])) 
        {
            temp += s[i];
        }
  
        // The sub-string containing
        // all vowels ends here
        else
        {
  
            // If there was a valid sub-string
            if (temp.Length > 0)
                count += countSubstringsUtil(temp.ToCharArray());
  
            // Reset temp string
            temp = "";
        }
    }
  
    // For the last valid sub-string
    if (temp.Length > 0)
        count += countSubstringsUtil(temp.ToCharArray());
  
    return count;
}
  
// Driver code
public static void Main(String[] args) 
{
    String s = "aeouisddaaeeiouua";
  
    Console.WriteLine(countSubstrings(s));
}
}
  
// This code is contributed by Princi Singh


输出:
9

时间复杂度: O(N)