📌  相关文章
📜  检查一个字符串可以分成两个具有相同元音数量的子字符串

📅  最后修改于: 2021-04-17 16:27:33             🧑  作者: Mango

给定字符串S ,任务是检查字符串可以拆分为两个子字符串,以使两个子字符串中的元音数量相等。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

天真的方法:解决此问题的最简单方法是遍历给定的字符串S,并将该字符串在每个可能的索引处分为两个子字符串。对于每个此类拆分,请检查两个子字符串中是否存在相同数量的元音。如果发现是真的,则打印“是”,否则打印“否”
时间复杂度: O(N 2 )
辅助空间: O(1)

高效的方法:可以通过预先计算字符串的元音总数来优化上述方法。请按照以下步骤解决问题:

  • 初始化两个变量,例如totalVowelsvowelsTillNow ,分别存储元音的总数和当前的元音计数。
  • 现在遍历字符串S并计算所有元音并将其存储在totalVowels中
  • 现在,再次通过1和增量vowelsTillNow1遍历字符串S和递减totalVowels,如果元音occurs.Check如果totalVowels变得在任何点或不等于vowelsTillNow。如果发现是真的,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if any
// character is a vowel or not
bool isVowel(char ch)
{
   
    // Lowercase vowels
    if (ch == 'a' || ch == 'e' || ch == 'i'
        || ch == 'o' || ch == 'u')
        return true;
 
    // Uppercase vowels
    if (ch == 'A' || ch == 'E' || ch == 'I'
        || ch == 'O' || ch == 'U')
        return true;
 
    // Otherwise
    return false;
}
 
// Function to check if string S
// can be split into two substrings
// with equal number of vowels
string containsEqualStrings(string S)
{
 
    // Stores the count of vowels
    // in the string S
    int totalVowels = 0;
 
    // Traverse over the string
    for (int i = 0;
         i < S.size(); i++)
    {
 
        // If S[i] is vowel
        if (isVowel(S[i]))
            totalVowels++;
    }
 
    // Stores the count of vowels
    // upto the current index
    int vowelsTillNow = 0;
 
    // Traverse over the string
    for (int i = 0;
         i < S.size(); i++)
    {
 
        // If S[i] is vowel
        if (isVowel(S[i]))
        {
            vowelsTillNow++;
            totalVowels--;
 
            // If vowelsTillNow and
            // totalVowels are equal
            if (vowelsTillNow
                == totalVowels)
            {
 
                return "Yes";
            }
        }
    }
 
    // Otherwise
    return "No";
}
 
// Driver Code
int main()
{
    string S = "geeks";
    cout<<(containsEqualStrings(S));
}
 
// This code is contributed by mohit kumar 29.


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to check if any
    // character is a vowel or not
    public static boolean isVowel(char ch)
    {
        // Lowercase vowels
        if (ch == 'a' || ch == 'e' || ch == 'i'
            || ch == 'o' || ch == 'u')
            return true;
 
        // Uppercase vowels
        if (ch == 'A' || ch == 'E' || ch == 'I'
            || ch == 'O' || ch == 'U')
            return true;
 
        // Otherwise
        return false;
    }
 
    // Function to check if string S
    // can be split into two substrings
    // with equal number of vowels
    public static String
    containsEqualStrings(String S)
    {
 
        // Stores the count of vowels
        // in the string S
        int totalVowels = 0;
 
        // Traverse over the string
        for (int i = 0;
             i < S.length(); i++) {
 
            // If S[i] is vowel
            if (isVowel(S.charAt(i)))
                totalVowels++;
        }
 
        // Stores the count of vowels
        // upto the current index
        int vowelsTillNow = 0;
 
        // Traverse over the string
        for (int i = 0;
             i < S.length(); i++) {
 
            // If S[i] is vowel
            if (isVowel(S.charAt(i))) {
                vowelsTillNow++;
                totalVowels--;
 
                // If vowelsTillNow and
                // totalVowels are equal
                if (vowelsTillNow
                    == totalVowels) {
 
                    return "Yes";
                }
            }
        }
 
        // Otherwise
        return "No";
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "geeks";
        System.out.println(
            containsEqualStrings(S));
    }
}


Python3
# Python3 program for the above approach
 
# Function to check if any
# character is a vowel or not
def isVowel(ch):
     
    # Lowercase vowels
    if (ch == 'a' or ch == 'e' or
        ch == 'i' or ch == 'o' or
        ch == 'u'):
        return True
 
    # Uppercase vowels
    if (ch == 'A' or ch == 'E' or
        ch == 'I' or ch == 'O' or
        ch == 'U'):
        return True
 
    # Otherwise
    return False
 
# Function to check if string S
# can be split into two substrings
# with equal number of vowels
def containsEqualStrings(S):
 
    # Stores the count of vowels
    # in the string S
    totalVowels = 0
 
    # Traverse over the string
    for i in range(len(S)):
 
        # If S[i] is vowel
        if (isVowel(S[i])):
            totalVowels += 1
 
    # Stores the count of vowels
    # upto the current index
    vowelsTillNow = 0
 
    # Traverse over the string
    for i in range(len(S)):
 
        # If S[i] is vowel
        if (isVowel(S[i])):
            vowelsTillNow += 1
            totalVowels -= 1
 
            # If vowelsTillNow and
            # totalVowels are equal
            if (vowelsTillNow == totalVowels):
                return "Yes"
 
    # Otherwise
    return "No"
 
# Driver Code
if __name__ == "__main__":
 
    S = "geeks"
    print(containsEqualStrings(S))
 
# This code is contributed by ukasp


C#
// C# program for the above approach
using System;
class GFG
{
 
    // Function to check if any
    // character is a vowel or not
    public static bool isVowel(char ch)
    {
       
        // Lowercase vowels
        if (ch == 'a' || ch == 'e' || ch == 'i'
            || ch == 'o' || ch == 'u')
            return true;
 
        // Uppercase vowels
        if (ch == 'A' || ch == 'E' || ch == 'I'
            || ch == 'O' || ch == 'U')
            return true;
 
        // Otherwise
        return false;
    }
 
    // Function to check if string S
    // can be split into two substrings
    // with equal number of vowels
    public static String
    containsEqualStrings(string S)
    {
 
        // Stores the count of vowels
        // in the string S
        int totalVowels = 0;
 
        // Traverse over the string
        for (int i = 0;
             i < S.Length; i++)
        {
 
            // If S[i] is vowel
            if (isVowel(S[i]))
                totalVowels++;
        }
 
        // Stores the count of vowels
        // upto the current index
        int vowelsTillNow = 0;
 
        // Traverse over the string
        for (int i = 0;
             i < S.Length; i++) {
 
            // If S[i] is vowel
            if (isVowel(S[i])) {
                vowelsTillNow++;
                totalVowels--;
 
                // If vowelsTillNow and
                // totalVowels are equal
                if (vowelsTillNow
                    == totalVowels) {
 
                    return "Yes";
                }
            }
        }
 
        // Otherwise
        return "No";
    }
 
// Driver Code
static public void Main()
{
    string S = "geeks";
    Console.WriteLine(
        containsEqualStrings(S));
}
}
 
// This code is contributed by code_hunt.


输出:
Yes

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