Determine if String Halves Are Alike - Problem
Imagine you have a perfectly balanced string of even length, like a word written on a piece of paper that you want to fold in half. Your task is to determine if both halves are "vowel-balanced" - meaning they contain the same number of vowels!
Given a string s of even length, split it into two equal halves: the first half and the second half. Two string halves are considered alike if they contain the same number of vowels.
Vowels include: 'a', 'e', 'i', 'o', 'u' and their uppercase versions 'A', 'E', 'I', 'O', 'U'
Return true if both halves are alike, false otherwise.
Input & Output
example_1.py โ Basic Case
$
Input:
s = "book"
โบ
Output:
true
๐ก Note:
First half: "bo" has 1 vowel ('o'). Second half: "ok" has 1 vowel ('o'). Since both halves have the same number of vowels (1), return true.
example_2.py โ Mixed Case
$
Input:
s = "textbook"
โบ
Output:
false
๐ก Note:
First half: "text" has 1 vowel ('e'). Second half: "book" has 2 vowels ('o', 'o'). Since the counts differ (1 โ 2), return false.
example_3.py โ All Consonants
$
Input:
s = "MzXy"
โบ
Output:
true
๐ก Note:
First half: "Mz" has 0 vowels. Second half: "Xy" has 0 vowels. Since both halves have the same number of vowels (0), return true.
Constraints
- 2 โค s.length โค 1000
- s.length is even
- s consists of uppercase and lowercase letters only
Visualization
Tap to expand
Understanding the Visualization
1
Initialize Scale
Start with a balanced scale (counter = 0)
2
Add First Half Vowels
Add +1 weight for each vowel in first half
3
Remove Second Half Vowels
Remove -1 weight for each vowel in second half
4
Check Balance
Scale is balanced (counter = 0) means equal vowels
Key Takeaway
๐ฏ Key Insight: Instead of counting vowels separately, we use a balance counter that naturally reaches zero when both halves have equal vowel counts!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code