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

📅  最后修改于: 2021-09-03 14:58:33             🧑  作者: Mango

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

例子:

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

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

  • 初始化两个变量,比如totalVowelsvueelsTillNow ,分别存储元音的总数和当前的元音数。
  • 现在遍历字符串S并计算所有元音并将其存储在totalVowels 中
  • 现在,再次遍历字符串S并将totalVowels递减1并且将vueelsTillNow递增1 ,如果出现元音。检查totalVowels是否在任何时候都等于vueelsTillNow 。如果发现是真的,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

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.


Javascript


输出:
Yes

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

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