📜  由偶数个元音组成的子串计数

📅  最后修改于: 2021-09-04 09:40:18             🧑  作者: Mango

给定一个长度为N的字符串S ,任务是找到具有偶数个元音的非空子串的数量。

例子:

天真的方法:
解决问题的最简单方法是生成给定字符串的所有可能子串,并为每个子串计算元音的数量并检查它是否为偶数。如果发现是偶数,则增加计数。最后,在检查所有子字符串后,打印count的值作为答案。

下面是上述方法的实现:

C++
// C++ program to implement
//the above approach
#include 
using namespace std;
 
// Utility function to check
// if a character is a vowel
bool isVowel(char c)
{
    if (c == 'a' || c == 'e' ||
        c == 'i' || c == 'o' ||
        c == 'u')
        return true;
 
    return false;
}
 
// Function to calculate and return the
// count of substrings with even number
// of vowels
void countSubstrings(string s, int n)
{
 
    // Stores the count of substrings
    int result = 0;
 
    for(int i = 0; i < n; i++)
    {
        int count = 0;
        for(int j = i; j < n; j++)
        {
 
            // If the current character
            // is a vowel
            if (isVowel(s[j]))
            {
                 
                // Increase count
                count++;
            }
 
            // If substring contains
            // even number of vowels
            if (count % 2 == 0)
 
                // Increase the answer
                result++;
        }
    }
 
    // Print the final answer
    cout << result;
}
 
// Driver Code
int main()
{
    int n = 5;
    string s = "abcde";
     
    countSubstrings(s, n);
    return 0;
}
 
// This code is contributed by Amit Katiyar


Java
// Java Program to implement
// the above approach
class GFG {
 
    // Utility function to check
    // if a character is a vowel
    static boolean isVowel(char c)
    {
        if (c == 'a' || c == 'e' || c == 'i'
            || c == 'o' || c == 'u')
            return true;
 
        return false;
    }
 
    // Function to calculate and return the
    // count of substrings with even number
    // of vowels
    static void countSubstrings(String s, int n)
    {
 
        // Stores the count of substrings
        int result = 0;
 
        for (int i = 0; i < n; i++) {
            int count = 0;
            for (int j = i; j < n; j++) {
 
                // If the current character
                // is a vowel
                if (isVowel(s.charAt(j))) {
 
                    // Increase count
                    count++;
                }
 
                // If substring contains
                // even number of vowels
                if (count % 2 == 0)
 
                    // Increase the answer
                    result++;
            }
        }
 
        // Print the final answer
        System.out.println(result);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 5;
        String s = "abcde";
        countSubstrings(s, n);
    }
}


Python3
# Python3 Program to implement
# the above approach
 
# Utility function to check
# if a character is a vowel
def isVowel(c):
 
    if (c == 'a' or c == 'e' or
        c == 'i' or c == 'o' or
        c == 'u'):
        return True
 
    return False
 
# Function to calculate and return the
# count of substrings with even number
# of vowels
def countSubstrings(s, n):
 
    # Stores the count of substrings
    result = 0
 
    for i in range(n):
        count = 0
        for j in range(i, n):
 
            # If the current character
            # is a vowel
            if (isVowel(s[j])):
 
                #Increase count
                count += 1
 
            # If substring contains
            # even number of vowels
            if (count % 2 == 0):
 
                #Increase the answer
                result += 1
 
    # Prthe final answer
    print(result)
 
# Driver Code
if __name__ == '__main__':
 
    n = 5
    s = "abcde"
    countSubstrings(s, n)
 
# This code is contributed by Mohit Kumar


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Utility function to check
// if a character is a vowel
static bool isVowel(char c)
{
    if (c == 'a' || c == 'e' || c == 'i' ||
        c == 'o' || c == 'u')
        return true;
 
    return false;
}
 
// Function to calculate and return the
// count of substrings with even number
// of vowels
static void countSubstrings(String s, int n)
{
 
    // Stores the count of substrings
    int result = 0;
 
    for(int i = 0; i < n; i++)
    {
        int count = 0;
        for(int j = i; j < n; j++)
        {
 
            // If the current character
            // is a vowel
            if (isVowel(s[j]))
            {
 
                // Increase count
                count++;
            }
 
            // If substring contains
            // even number of vowels
            if (count % 2 == 0)
 
                // Increase the answer
                result++;
        }
    }
 
    // Print the final answer
    Console.WriteLine(result);
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 5;
    String s = "abcde";
     
    countSubstrings(s, n);
}
}
 
// This code is contributed by amal kumar choubey


Javascript


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Utility function to check
// if a character is a vowel
bool isVowel(char c)
{
    if (c == 'a' || c == 'e' ||
        c == 'i' || c == 'o' ||
        c == 'u')
        return true;
 
    return false;
}
 
// Function to calculate and return the
// count of substrings with even number
// of vowels
void countSubstrings(string s, int n)
{
     
    // Stores the count of substrings
    // with even and odd number of
    // vowels respectively
    int temp[] = { 1, 0 };
 
    int result = 0, sum = 0;
    for(int i = 0; i <= n - 1; i++)
    {
         
        // Update count of vowels modulo 2
        // in sum to obtain even or odd
        sum += (isVowel(s[i]) ? 1 : 0);
        sum %= 2;
 
        // Increment even/odd count
        temp[sum]++;
    }
 
    // Count substrings with even number
    // of vowels using Handshaking Lemma
    result += ((temp[0] * (temp[0] - 1)) / 2);
    result += ((temp[1] * (temp[1] - 1)) / 2);
 
    cout << result;
}
 
// Driver Code
int main()
{
    int n = 5;
    string s = "abcde";
     
    countSubstrings(s, n);
}
 
// This code is contributed by Amit Katiyar


Java
// Java Program to implement
// the above approach
class GFG {
 
    // Utility function to check
    // if a character is a vowel
    static boolean isVowel(char c)
    {
        if (c == 'a' || c == 'e' || c == 'i'
            || c == 'o' || c == 'u')
            return true;
 
        return false;
    }
 
    // Function to calculate and return the
    // count of substrings with even number
    // of vowels
    static void countSubstrings(String s, int n)
    {
        // Stores the count of substrings
        // with even and odd number of
        // vowels respectively
        int temp[] = { 1, 0 };
 
        int result = 0, sum = 0;
        for (int i = 0; i <= n - 1; i++) {
 
            // Update count of vowels modulo 2
            // in sum to obtain even or odd
            sum += (isVowel(s.charAt(i)) ? 1 : 0);
            sum %= 2;
 
            // Increment even/odd count
            temp[sum]++;
        }
 
        // Count substrings with even number
        // of vowels using Handshaking Lemma
        result += ((temp[0] * (temp[0] - 1)) / 2);
        result += ((temp[1] * (temp[1] - 1)) / 2);
 
        System.out.println(result);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 5;
        String s = "abcde";
        countSubstrings(s, n);
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Utility function to check
# if a character is a vowel
def isVowel(c):
     
    if (c == 'a' or c == 'e' or
        c == 'i' or c == 'o' or
        c == 'u'):
        return True;
 
    return False;
 
# Function to calculate and return the
# count of substrings with even number
# of vowels
def countSubstrings(s, n):
     
    # Stores the count of substrings
    # with even and odd number of
    # vowels respectively
    temp = [1, 0];
 
    result = 0;
    sum = 0;
    for i in range(0, n):
         
        # Update count of vowels modulo 2
        # in sum to obtain even or odd
        sum += (1 if isVowel(s[i]) else 0);
        sum %= 2;
 
        # Increment even/odd count
        temp[sum] += 1;
 
    # Count substrings with even number
    # of vowels using Handshaking Lemma
    result += ((temp[0] * (temp[0] - 1)) // 2);
    result += ((temp[1] * (temp[1] - 1)) // 2);
 
    print(result);
 
# Driver Code
if __name__ == '__main__':
     
    n = 5;
    s = "abcde";
     
    countSubstrings(s, n);
 
# This code is contributed by amal kumar choubey


C#
// C# Program to implement
// the above approach
using System;
class GFG {
  
  // Utility function to check
  // if a character is a vowel
  static bool isVowel(char c)
  {
    if (c == 'a' || c == 'e' || c == 'i' ||
        c == 'o' || c == 'u')
      return true;
 
    return false;
  }
 
  // Function to calculate and return the
  // count of substrings with even number
  // of vowels
  static void countSubstrings(String s, int n)
  {
    // Stores the count of substrings
    // with even and odd number of
    // vowels respectively
    int []temp = { 1, 0 };
 
    int result = 0, sum = 0;
    for (int i = 0; i <= n - 1; i++)
    {
      // Update count of vowels modulo 2
      // in sum to obtain even or odd
      sum += (isVowel(s[i]) ? 1 : 0);
      sum %= 2;
 
      // Increment even/odd count
      temp[sum]++;
    }
 
    // Count substrings with even number
    // of vowels using Handshaking Lemma
    result += ((temp[0] * (temp[0] - 1)) / 2);
    result += ((temp[1] * (temp[1] - 1)) / 2);
 
    Console.Write(result);
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int n = 5;
    String s = "abcde";
    countSubstrings(s, n);
  }
}
 
// This code is contributed by rock_cool


Javascript


输出:
7

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

有效的方法:
按照以下步骤优化上述方法:

  • 初始化累积计数模数组temp[] ,使得:
  • 如果[S 0 , S i-1 ] 中的元音数与[S 0 , S j ] 中的元音数具有相同的奇偶性,则任何子串[S i , S j ]将包含偶数个元音,即他们要么都是偶数,要么都是奇数
  • 由于元音为偶数和元音为奇数的子串的计数存储在temp[] 中,因此,通过使用握手引理

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Utility function to check
// if a character is a vowel
bool isVowel(char c)
{
    if (c == 'a' || c == 'e' ||
        c == 'i' || c == 'o' ||
        c == 'u')
        return true;
 
    return false;
}
 
// Function to calculate and return the
// count of substrings with even number
// of vowels
void countSubstrings(string s, int n)
{
     
    // Stores the count of substrings
    // with even and odd number of
    // vowels respectively
    int temp[] = { 1, 0 };
 
    int result = 0, sum = 0;
    for(int i = 0; i <= n - 1; i++)
    {
         
        // Update count of vowels modulo 2
        // in sum to obtain even or odd
        sum += (isVowel(s[i]) ? 1 : 0);
        sum %= 2;
 
        // Increment even/odd count
        temp[sum]++;
    }
 
    // Count substrings with even number
    // of vowels using Handshaking Lemma
    result += ((temp[0] * (temp[0] - 1)) / 2);
    result += ((temp[1] * (temp[1] - 1)) / 2);
 
    cout << result;
}
 
// Driver Code
int main()
{
    int n = 5;
    string s = "abcde";
     
    countSubstrings(s, n);
}
 
// This code is contributed by Amit Katiyar

Java

// Java Program to implement
// the above approach
class GFG {
 
    // Utility function to check
    // if a character is a vowel
    static boolean isVowel(char c)
    {
        if (c == 'a' || c == 'e' || c == 'i'
            || c == 'o' || c == 'u')
            return true;
 
        return false;
    }
 
    // Function to calculate and return the
    // count of substrings with even number
    // of vowels
    static void countSubstrings(String s, int n)
    {
        // Stores the count of substrings
        // with even and odd number of
        // vowels respectively
        int temp[] = { 1, 0 };
 
        int result = 0, sum = 0;
        for (int i = 0; i <= n - 1; i++) {
 
            // Update count of vowels modulo 2
            // in sum to obtain even or odd
            sum += (isVowel(s.charAt(i)) ? 1 : 0);
            sum %= 2;
 
            // Increment even/odd count
            temp[sum]++;
        }
 
        // Count substrings with even number
        // of vowels using Handshaking Lemma
        result += ((temp[0] * (temp[0] - 1)) / 2);
        result += ((temp[1] * (temp[1] - 1)) / 2);
 
        System.out.println(result);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 5;
        String s = "abcde";
        countSubstrings(s, n);
    }
}

蟒蛇3

# Python3 program to implement
# the above approach
 
# Utility function to check
# if a character is a vowel
def isVowel(c):
     
    if (c == 'a' or c == 'e' or
        c == 'i' or c == 'o' or
        c == 'u'):
        return True;
 
    return False;
 
# Function to calculate and return the
# count of substrings with even number
# of vowels
def countSubstrings(s, n):
     
    # Stores the count of substrings
    # with even and odd number of
    # vowels respectively
    temp = [1, 0];
 
    result = 0;
    sum = 0;
    for i in range(0, n):
         
        # Update count of vowels modulo 2
        # in sum to obtain even or odd
        sum += (1 if isVowel(s[i]) else 0);
        sum %= 2;
 
        # Increment even/odd count
        temp[sum] += 1;
 
    # Count substrings with even number
    # of vowels using Handshaking Lemma
    result += ((temp[0] * (temp[0] - 1)) // 2);
    result += ((temp[1] * (temp[1] - 1)) // 2);
 
    print(result);
 
# Driver Code
if __name__ == '__main__':
     
    n = 5;
    s = "abcde";
     
    countSubstrings(s, n);
 
# This code is contributed by amal kumar choubey

C#

// C# Program to implement
// the above approach
using System;
class GFG {
  
  // Utility function to check
  // if a character is a vowel
  static bool isVowel(char c)
  {
    if (c == 'a' || c == 'e' || c == 'i' ||
        c == 'o' || c == 'u')
      return true;
 
    return false;
  }
 
  // Function to calculate and return the
  // count of substrings with even number
  // of vowels
  static void countSubstrings(String s, int n)
  {
    // Stores the count of substrings
    // with even and odd number of
    // vowels respectively
    int []temp = { 1, 0 };
 
    int result = 0, sum = 0;
    for (int i = 0; i <= n - 1; i++)
    {
      // Update count of vowels modulo 2
      // in sum to obtain even or odd
      sum += (isVowel(s[i]) ? 1 : 0);
      sum %= 2;
 
      // Increment even/odd count
      temp[sum]++;
    }
 
    // Count substrings with even number
    // of vowels using Handshaking Lemma
    result += ((temp[0] * (temp[0] - 1)) / 2);
    result += ((temp[1] * (temp[1] - 1)) / 2);
 
    Console.Write(result);
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int n = 5;
    String s = "abcde";
    countSubstrings(s, n);
  }
}
 
// This code is contributed by rock_cool

Javascript


输出:
7

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live