檢查字串是否可以拆分為兩個母音數量相等的子字串


歡迎來到另一篇關於 C++ 中一個引人入勝的解決問題主題的深入指南。這次,我們將解決確定一個字串是否可以分成兩個子字串的問題,每個子字串包含相同數量的母音。這個問題是磨練您在字串操作和母音計數方面的技能的絕佳練習。

問題陳述

給定一個字串,我們的目標是確定它是否可以被劃分為兩個非空子字串,使得這兩個子字串具有相同數量的母音。英語字母表中的母音是 'a'、'e'、'i'、'o'、'u'、'A'、'E'、'I'、'O'、'U'。

方法

我們的方法是首先計算字串中母音的總數。如果總數不是偶數,我們立即知道無法將字串拆分為兩個母音數量相等的子字串。

如果總數是偶數,那麼我們遍歷字串,並對遇到的母音進行計數。如果在任何時候我們的計數等於總數的一半,我們可以在該點將字串拆分為兩個母音數量相等的子字串。

示例

以下是上述方法的程式 -

#include <stdio.h>
#include <stdbool.h>

// Function to check if a character is a vowel.
bool isVowel(char c) {
   return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || 
   c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
}

// Function to determine if a string can be split into two substrings with an equal number of vowels.
bool canBeSplit(char *s) {
   int totalVowels = 0;

   // Count the total number of vowels in the string.
   for (int i = 0; s[i] != '\0'; i++) {
      if (isVowel(s[i]))
      totalVowels++;
   }

   if (totalVowels % 2 != 0)
   return false;

   int halfVowels = 0;

   for (int i = 0; s[i] != '\0'; i++) {
      if (isVowel(s[i]))
         halfVowels++;
      if (halfVowels == totalVowels / 2)
         return true; // If half the vowels are encountered, return true.
   }

   return false; // If half the vowels aren't encountered by the end, return false.
}
int main() {
   char s[] = "CIVIC";

   if (canBeSplit(s))
      printf("Yes, the string can be split into two substrings with an equal number of vowels.\n");
   else
      printf("No, the string cannot be split into two substrings with an equal number of vowels.\n");

   return 0;
}

輸出

Yes, the string can be split into two substrings with an equal number of vowels.
#include<iostream>
using namespace std;

// Function to check if a character is a vowel.
bool isVowel(char c) {
   return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || 
   c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
}

// Function to determine if a string can be split into two substrings with an equal number of vowels.
bool canBeSplit(string s) {
   int totalVowels = 0;

   // Count the total number of vowels in the string.
   for (char c : s) {
      if (isVowel(c))
      totalVowels++;
   }


   if (totalVowels % 2 != 0)
   return false;

   int halfVowels = 0;

   for (char c : s) {
      if (isVowel(c))
         halfVowels++;
      if (halfVowels == totalVowels / 2)
   return true; // If half the vowels are encountered, return true.
   }

   return false; // If half the vowels aren't encountered by the end, return false.
}

int main() {
   string s = "CIVIC";

   if (canBeSplit(s))
      cout << "Yes, the string can be split into two substrings with an equal number of vowels." << endl;
   else
      cout << "No, the string cannot be split into two substrings with an equal number of vowels." << endl;

   return 0; 
}

輸出

Yes, the string can be split into two substrings with an equal number of vowels.
public class VowelSplit {
   // Function to check if a character is a vowel.
   static boolean isVowel(char c) {
      return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
      c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
   }

   // Function to determine if a string can be split into two substrings with an equal number of vowels.
   static boolean canBeSplit(String s) {
      int totalVowels = 0;

      // Count the total number of vowels in the string.
      for (char c : s.toCharArray()) {
         if (isVowel(c))
         totalVowels++;
      }

      if (totalVowels % 2 != 0)
         return false;

      int halfVowels = 0;

      for (char c : s.toCharArray()) {
         if (isVowel(c))
            halfVowels++;
         if (halfVowels == totalVowels / 2)
            return true; // If half the vowels are encountered, return true.
      }

      return false; // If half the vowels aren't encountered by the end, return false.
   }

   public static void main(String[] args) {
      String s = "CIVIC";

      if (canBeSplit(s))
         System.out.println("Yes, the string can be split into two substrings with an equal number of vowels.");
      else
         System.out.println("No, the string cannot be split into two substrings with an equal number of vowels.");
   }
}

輸出

Yes, the string can be split into two substrings with an equal number of vowels.
def is_vowel(c):
   return c in 'aeiouAEIOU'

def can_be_split(s):
   total_vowels = sum(1 for c in s if is_vowel(c))

   if total_vowels % 2 != 0:
      return False

   half_vowels = 0

   for c in s:
      if is_vowel(c):
         half_vowels += 1
      if half_vowels == total_vowels / 2:
         return True

   return False

s = "CIVIC"

if can_be_split(s):
   print("Yes, the string can be split into two substrings with an equal number of vowels.")
else:
   print("No, the string cannot be split into two substrings with an equal number of vowels.")

輸出

Yes, the string can be split into two substrings with an equal number of vowels.

測試用例示例

讓我們用一個例子來說明這個問題及其解決方案 -

假設字串為“beautiful”。

  • 我們首先計算“beautiful”中母音的總數,即 5。由於這不是偶數,我們立即知道該字串不能拆分為兩個母音數量相等的子字串。

  • 因此,輸出將是“否,該字串不能拆分為兩個母音數量相等的子字串。”

結論

透過本 C++ 指南,我們學習瞭如何檢查字串是否可以分成兩個子字串,以便每個子字串包含相同數量的母音。此問題是在 C++ 語言中進行字串操作和字元計數的有用練習。

更新於: 2023年10月16日

309 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告