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
Balance Scale VisualizationFirst Half+1 (A)Second Half-1 (E)11Balanced! โš–๏ธString: "AbCdEfGh" โ†’ Balance: +1 - 1 = 0
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!
Asked in
Facebook 25 Amazon 18 Google 15 Microsoft 12
89.2K Views
Medium Frequency
~8 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen