Check if a string can be split into two substrings with equal number of vowels


Welcome to another in-depth guide on a fascinating problem-solving topic in C++. This time, we will be tackling the problem of determining if a string can be divided into two substrings, each containing an equal number of vowels. This problem is an excellent exercise for honing your skills in string manipulation and vowel counting.

Problem Statement

Given a string, our objective is to determine if it can be partitioned into two non-empty substrings such that both substrings have an equal number of vowels. The vowels in the English alphabet are 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'.

Approach

Our approach is to first count the total number of vowels in the string. If the total count is not an even number, we immediately know that it's impossible to split the string into two substrings with an equal number of vowels.

If the total count is an even number, we then iterate through the string, keeping a running count of the vowels we encounter. If at any point our running count equals half of the total count, we can split the string at that point into two substrings with an equal number of vowels.

Example

Here is the C++ code to solve this problem 

#include<bits/stdc++.h>
using namespace std;

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');
}

bool canBeSplit(string s) {
   int totalVowels = 0;
   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;
   }
   return false;
}

int main() {
   string s="beautiful";
   if (canBeSplit(s))
      cout << "Yes, the string can be split into two substrings with equal number of vowels." << endl;
   else
      cout << "No, the string cannot be split into two substrings with equal number of vowels." << endl;
   return 0;
}

Output

No, the string cannot be split into two substrings with equal number of vowels.

Test Case Example

Let's illustrate this problem and its solution with an example −

Suppose the string is "beautiful".

  • We first count the total number of vowels in "beautiful", which is 5. Since this is not an even number, we immediately know that the string cannot be split into two substrings with an equal number of vowels.

  • Therefore, the output will be "No, the string cannot be split into two substrings with equal number of vowels."

Conclusion

Through this C++ guide, we have learned how to check if a string can be divided into two substrings such that each substring contains an equal number of vowels. This problem is a useful exercise in string manipulation and character counting in the C++ language.

Updated on: 17-May-2023

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements