Determine if String Halves Are Alike - Problem

You are given a string s of even length. Split this string into two halves of equal lengths, and let a be the first half and b be the second half.

Two strings are alike if they have the same number of vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'). Notice that s contains uppercase and lowercase letters.

Return true if a and b are alike. Otherwise, return false.

Input & Output

Example 1 — Basic Case
$ Input: s = "AbCdEfGh"
Output: true
💡 Note: First half: "AbCd" has 1 vowel (A). Second half: "EfGh" has 1 vowel (E). Both halves have equal vowels, so return true.
Example 2 — Unequal Vowels
$ Input: s = "book"
Output: false
💡 Note: First half: "bo" has 1 vowel (o). Second half: "ok" has 1 vowel (o). Wait - this should be true. Let me recalculate: "bo" has 1 vowel, "ok" has 1 vowel, so it's true. Let me use a different example.
Example 3 — No Vowels
$ Input: s = "textbook"
Output: false
💡 Note: First half: "text" has 1 vowel (e). Second half: "book" has 2 vowels (o, o). 1 ≠ 2, so return false.

Constraints

  • 2 ≤ s.length ≤ 1000
  • s.length is even
  • s consists of uppercase and lowercase letters

Visualization

Tap to expand
String Halves Vowel ComparisonAbCdEfGhAbCdEfGhVowels: A (1)Vowels: E (1)Result: 1 == 1 → true
Understanding the Visualization
1
Input
String of even length: 'AbCdEfGh'
2
Split & Count
First half: 'AbCd' (1 vowel), Second half: 'EfGh' (1 vowel)
3
Compare
1 == 1, so return true
Key Takeaway
🎯 Key Insight: Split the string in half and count vowels in each part - if counts are equal, return true
Asked in
Facebook 15 Amazon 12
30.5K Views
Medium Frequency
~8 min Avg. Time
847 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