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

Following are the programs to the above approach −

#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;
}

Output

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

Output

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.");
   }
}

Output

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.")

Output

Yes, the string can be split into two substrings with an 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: 16-Oct-2023

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements