Skip to main content
Back to problems
#1704
Easy Algorithms

Determine if string halves are alike

String Counting
78.8% acceptance
Feb 25, 2026
2332
126
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn halves_are_alike(s: String) -> bool {
    let is_vowel = |c: u8| matches!(c, b'a' | b'e' | b'i' | b'o' | b'u' | b'A' | b'E' | b'I' | b'O' | b'U');
    let bytes = s.as_bytes();
    let half = bytes.len() / 2;
    let a = bytes[..half].iter().filter(|&&c| is_vowel(c)).count();
    let b = bytes[half..].iter().filter(|&&c| is_vowel(c)).count();
    a == b
  }
}