Vowel Balance

Today’s challenge is about checking whether a string has balanced vowels. At first glance, this may feel like a simple challenge and this may be the first step that you may put to string manipulation.
So let’s see the actual problem,
Given a string, determine whether the number of vowels in the first half of the string is equal to the number of vowels in the second half.
• The string can contain any characters.
• The letters a, e, i, o, and u, in either uppercase or lowercase, are considered vowels.
• If there's an odd number of characters in the string, ignore the center character.
When I first read the problem what I thought was to divide the particular string into two parts and then count the vowels separately and then compare whether the count is equal or not.
If you start to think about it more you will see another approach. Let’s pick two pointers one at the starting position of the string and other one is at the ending of the string. Then the pointer that is at the starting of the string is counting upward until the end of the string and the other pointer moves backward from the end of the string. When both of them meet each other we divided the string into two parts. This is one of many methods that can be used to solve this type of a problem. You may find another more efficient methods as well.
Let’s look at the code.
def is_balanced(s):
s_no_spaces = s.replace(" ", "") # remove spaces
s = s_no_spaces.lower() # convert to lowercase
vowels = "aeiou"
i=0
j=len(s)-1
no_of_vowels_lower=0
no_of_vowels_upper=0
while i < j:
if s[i] in vowels:
no_of_vowels_lower += 1
if s[j] in vowels:
no_of_vowels_upper += 1
i += 1
j -= 1
return no_of_vowels_lower == no_of_vowels_upper
First we need to remove the spaces and then covert that string into lowercase because my vowel string is in lowercase. It we update vowel string with uppercase letters then we do not need to lowercase the incoming string.
Then we have the two pointers which are pointing to the starting and ending of the incoming string. And we have two lines to count the vowels in the left side and the right side.
Next there is a While loop which loops until “i” value is less than “j” value. Inside the while loop there are two if condition checks whether to identify a particular character in the “s” string is a vowel or not by using "“in” which will loops through the vowels string. Then there are two line to increment and decrement the values of “i” and “j”.
And finally the function returns boolean value of the result of “no_of_vowels_lower == no_of_vowels_upper”. It will be either True or False.
Example
Let’s take Hello World for the example.
First spacing and capitalization will be updated. Then we will get helloworld.
Then there will be two pointers and “i” will point to the character “h” and “j” will point to the d”. “h” and “d” are not vowels. So value of “i” will increment and value of “j” will decrement. Since there are “if” statements inside those will count the number of vowels it encountered.

When “i” points to character “o” and “j” points to character “w” the “while” loop will break. Then it will compare and return True or False.
In the example it will return as “False”.
I believe you got a clear idea about the problem and the solution for the problem. There are many ways of solving this problem.
This challenge may seem small, but it introduces powerful techniques like two-pointer traversal, which you’ll see again in many algorithm problems.
