Check if a string can be split into two substrings with equal number of vowels
Given a string S, the task is to check if the string can be split into two substrings such that the number of vowels in both of them are equal. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: S = “geeks”
Output: Yes
Explanation: Splitting the strings into substrings “ge” (count of vowels = 1) and “eks” (count of vowels = 1) satisfies the condition.Input: S = “textbook”
Output: No
Naive Approach: The simplest approach to solve this problem is to traverse the given string S and partition the string into two substrings at every possible index. For each such split, check if the two substrings have the same number of vowels present in them or not. If found to be true, then print “Yes” else print “No”.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by pre-calculating the total number of vowels in the string. Follow the steps below to solve the problem:
- Initialize two variables, say totalVowels and vowelsTillNow, to store the total number of vowels and the current count of vowels respectively.
- Now traverse the string S and count all the vowels and store it in the totalVowels.
- Now, again traverse the string S and decrement totalVowels by 1 and increment vowelsTillNow by 1, if a vowel occurs. Check if totalVowels become equal to vowelsTillNow at any point or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> 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
<script> // JavaScript program for the above approach // Function to check if any // character is a vowel or not function isVowel(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 function containsEqualStrings(S) { // Stores the count of vowels // in the string S var totalVowels = 0; // Traverse over the string for ( var 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 var vowelsTillNow = 0; // Traverse over the string for ( var 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 var S = "geeks" ; document.write(containsEqualStrings(S)); </script> |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)