Check if a string can be split into two substrings with equal number of vowels
Last Updated :
07 Nov, 2023
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”.
C++
#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' );
}
int containsEqualStrings(string S)
{
int n = S.length();
bool found = false ;
for ( int i = 0; i < n - 1; i++) {
int vowelsLeft = 0, vowelsRight = 0;
for ( int j = 0; j <= i; j++) {
if (isVowel(S[j])) {
vowelsLeft++;
}
}
for ( int j = i + 1; j < n; j++) {
if (isVowel(S[j])) {
vowelsRight++;
}
}
if (vowelsLeft == vowelsRight) {
found = true ;
break ;
}
}
if (found) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
int main()
{
string S = "geeks" ;
containsEqualStrings(S);
}
|
Java
import java.io.*;
import java.util.*;
public class Main {
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' );
}
static int containsEqualStrings(String S)
{
int n = S.length();
boolean found = false ;
for ( int i = 0 ; i < n - 1 ; i++) {
int vowelsLeft = 0 , vowelsRight = 0 ;
for ( int j = 0 ; j <= i; j++) {
if (isVowel(S.charAt(j))) {
vowelsLeft++;
}
}
for ( int j = i + 1 ; j < n; j++) {
if (isVowel(S.charAt(j))) {
vowelsRight++;
}
}
if (vowelsLeft == vowelsRight) {
found = true ;
break ;
}
}
if (found) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
return 0 ;
}
public static void main(String[] args)
{
String S = "geeks" ;
containsEqualStrings(S);
}
}
|
Python3
def isVowel(c):
return (c = = 'a' or c = = 'e' or c = = 'i' or c = = 'o'
or c = = 'u' or c = = 'A' or c = = 'E' or c = = 'I'
or c = = 'O' or c = = 'U' )
def containsEqualStrings( str ):
n = len ( str )
found = False
for i in range (n - 1 ):
vowelsLeft = 0
vowelsRight = 0
for j in range (i + 1 ):
if isVowel( str [j]):
vowelsLeft + = 1
for j in range (i + 1 , n):
if isVowel( str [j]):
vowelsRight + = 1
if vowelsLeft = = vowelsRight:
found = True
break
if found:
print ( "Yes" )
else :
print ( "No" )
str = "geeks"
containsEqualStrings( str )
|
C#
using System;
class Program {
static 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' );
}
static void ContainsEqualStrings( string S)
{
int n = S.Length;
bool found = false ;
for ( int i = 0; i < n - 1; i++) {
int vowelsLeft = 0, vowelsRight = 0;
for ( int j = 0; j <= i; j++) {
if (IsVowel(S[j])) {
vowelsLeft++;
}
}
for ( int j = i + 1; j < n; j++) {
if (IsVowel(S[j])) {
vowelsRight++;
}
}
if (vowelsLeft == vowelsRight) {
found = true ;
break ;
}
}
if (found) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
static void Main()
{
string S = "geeks" ;
ContainsEqualStrings(S);
}
}
|
Javascript
function isVowel(c) {
return (c === 'a' || c === 'e' || c === 'i' || c === 'o' ||
c === 'u' || c === 'A' || c === 'E' || c === 'I' ||
c === 'O' || c === 'U' );
}
function containsEqualStrings(S) {
const n = S.length;
let found = false ;
for (let i = 0; i < n - 1; i++) {
let vowelsLeft = 0, vowelsRight = 0;
for (let j = 0; j <= i; j++) {
if (isVowel(S[j])) {
vowelsLeft++;
}
}
for (let j = i + 1; j < n; j++) {
if (isVowel(S[j])) {
vowelsRight++;
}
}
if (vowelsLeft === vowelsRight) {
found = true ;
break ;
}
}
if (found) {
console.log( "Yes" );
}
else {
console.log( "No" );
}
}
const S = "geeks" ;
containsEqualStrings(S);
|
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++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u' )
return true ;
if (ch == 'A' || ch == 'E' || ch == 'I'
|| ch == 'O' || ch == 'U' )
return true ;
return false ;
}
string containsEqualStrings(string S)
{
int totalVowels = 0;
for ( int i = 0;
i < S.size(); i++)
{
if (isVowel(S[i]))
totalVowels++;
}
int vowelsTillNow = 0;
for ( int i = 0;
i < S.size(); i++)
{
if (isVowel(S[i]))
{
vowelsTillNow++;
totalVowels--;
if (vowelsTillNow
== totalVowels)
{
return "Yes" ;
}
}
}
return "No" ;
}
int main()
{
string S = "geeks" ;
cout<<(containsEqualStrings(S));
}
|
Java
import java.io.*;
class GFG {
public static boolean isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u' )
return true ;
if (ch == 'A' || ch == 'E' || ch == 'I'
|| ch == 'O' || ch == 'U' )
return true ;
return false ;
}
public static String
containsEqualStrings(String S)
{
int totalVowels = 0 ;
for ( int i = 0 ;
i < S.length(); i++) {
if (isVowel(S.charAt(i)))
totalVowels++;
}
int vowelsTillNow = 0 ;
for ( int i = 0 ;
i < S.length(); i++) {
if (isVowel(S.charAt(i))) {
vowelsTillNow++;
totalVowels--;
if (vowelsTillNow
== totalVowels) {
return "Yes" ;
}
}
}
return "No" ;
}
public static void main(String[] args)
{
String S = "geeks" ;
System.out.println(
containsEqualStrings(S));
}
}
|
Python3
def isVowel(ch):
if (ch = = 'a' or ch = = 'e' or
ch = = 'i' or ch = = 'o' or
ch = = 'u' ):
return True
if (ch = = 'A' or ch = = 'E' or
ch = = 'I' or ch = = 'O' or
ch = = 'U' ):
return True
return False
def containsEqualStrings(S):
totalVowels = 0
for i in range ( len (S)):
if (isVowel(S[i])):
totalVowels + = 1
vowelsTillNow = 0
for i in range ( len (S)):
if (isVowel(S[i])):
vowelsTillNow + = 1
totalVowels - = 1
if (vowelsTillNow = = totalVowels):
return "Yes"
return "No"
if __name__ = = "__main__" :
S = "geeks"
print (containsEqualStrings(S))
|
C#
using System;
class GFG
{
public static bool isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u' )
return true ;
if (ch == 'A' || ch == 'E' || ch == 'I'
|| ch == 'O' || ch == 'U' )
return true ;
return false ;
}
public static String
containsEqualStrings( string S)
{
int totalVowels = 0;
for ( int i = 0;
i < S.Length; i++)
{
if (isVowel(S[i]))
totalVowels++;
}
int vowelsTillNow = 0;
for ( int i = 0;
i < S.Length; i++) {
if (isVowel(S[i])) {
vowelsTillNow++;
totalVowels--;
if (vowelsTillNow
== totalVowels) {
return "Yes" ;
}
}
}
return "No" ;
}
static public void Main()
{
string S = "geeks" ;
Console.WriteLine(
containsEqualStrings(S));
}
}
|
Javascript
<script>
function isVowel(ch) {
if (ch === "a" || ch === "e" || ch === "i" || ch === "o" || ch === "u" )
return true ;
if (ch === "A" || ch === "E" || ch === "I" || ch === "O" || ch === "U" )
return true ;
return false ;
}
function containsEqualStrings(S) {
var totalVowels = 0;
for ( var i = 0; i < S.length; i++) {
if (isVowel(S[i])) totalVowels++;
}
var vowelsTillNow = 0;
for ( var i = 0; i < S.length; i++) {
if (isVowel(S[i])) {
vowelsTillNow++;
totalVowels--;
if (vowelsTillNow === totalVowels) {
return "Yes" ;
}
}
}
return "No" ;
}
var S = "geeks" ;
document.write(containsEqualStrings(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach 3: Count the number of vowels if the count is even then it is possible that we can divide it into two sub strings having same vowels else not.
This is simple and efficient approach. The following steps to follow to solve the problem:
1. Define a function “isVowel” that returns true if the input character is a vowel else false.
2. Define another function “isEqualVowels” that takes a string input “s” and counts the number of vowels present in the string.
3. If the count of vowels is even, print “Yes” ( It means we can divide vowels into two sub-strings). If the count of vowels is odd, print “No”.
4.In the main function, taken a string “s” and call the “isEqualVowels” function with the string as an argument.
Below is the implementation of the above approach:
C++
#include <iostream>
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' );
}
void isEqualVowels(string s)
{
int len = s.length();
int count_vowels = 0;
for ( int i = 0; i<len; i++)
{
if (isVowel(s[i]))
count_vowels++;
}
if (count_vowels%2==0)
cout<< "Yes" <<endl;
else
cout<< "No" <<endl;
}
int main() {
string s = "geeks" ;
isEqualVowels(s);
return 0;
}
|
Java
import java.io.*;
class GFG {
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' );
}
static void isEqualVowels(String s)
{
int len = s.length();
int count_vowels = 0 ;
for ( int i = 0 ; i<len; i++)
{
if (isVowel(s.charAt(i)))
count_vowels++;
}
if (count_vowels% 2 == 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String []args)
{
String s = "geeks" ;
isEqualVowels(s);
}
}
|
Python3
def is_vowel(c):
return c.lower() in [ 'a' , 'e' , 'i' , 'o' , 'u' ]
def is_equal_vowels(s):
count_vowels = sum ( 1 for char in s if is_vowel(char))
if count_vowels % 2 = = 0 :
print ( "Yes" )
else :
print ( "No" )
def main():
s = "geeks"
is_equal_vowels(s)
if __name__ = = "__main__" :
main()
|
C#
using System;
class Program
{
static 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' );
}
static void IsEqualVowels( string s)
{
int len = s.Length;
int countVowels = 0;
for ( int i = 0; i < len; i++)
{
if (IsVowel(s[i]))
countVowels++;
}
if (countVowels % 2 == 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static void Main()
{
string s = "geeks" ;
IsEqualVowels(s);
}
}
|
Javascript
function isVowel( c)
{
return (c == 'a' || c == 'e' || c == 'i' || c == 'o'
|| c == 'u' || c == 'A' || c == 'E' || c == 'I'
|| c == 'O' || c == 'U' );
}
function isEqualVowels( s)
{
let len = s.length;
let count_vowels = 0;
for (let i = 0; i<len; i++)
{
if (isVowel(s[i]))
count_vowels++;
}
if (count_vowels%2==0)
document.write( "Yes" );
else
document.write( "No" );
}
let s = "geeks" ;
isEqualVowels(s);
|
Time Complexity: O(N), as traversing over the string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...