Given a string s of even length, split it into two halves of equal length. Return true if both halves have the same number of vowels (a, e, i, o, u, case-insensitive). If the string length is odd, it cannot have equal halves — but for this problem, the input will always have even length.
Count vowels in the first half and in the second half. Compare the counts. Vowels are 'a', 'e', 'i', 'o', 'u' and their uppercase equivalents. Use a vowels set for quick lookup.
Time complexity is O(n) and space is O(1). Only half-length iterations and a vowel lookup set are needed.
Example Input & Output
'a' and 'A' both have 1 vowel
'ab' has 1 vowel, 'cd' has 0 vowels
'bo' has 1 vowel, 'ok' has 1 vowel
Empty: both halves have 0 vowels
'text' has 1 vowel, 'book' has 2 vowels
Algorithm Flow

Solution Approach
Best Answers
class Solution {
public boolean solution(String s) {
String v="aeiouAEIOU";int n=s.length()/2,l=0,r=0;
for(int i=0;i<n;i++){if(v.indexOf(s.charAt(i))!=-1)l++;if(v.indexOf(s.charAt(n+i))!=-1)r++;}
return l==r;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
