Determine if String Halves Are Alike

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
    def halvesAreAlike(self, s: str) -> bool:
        vowels = set('aeiouAEIOU')
        half_length = len(s) // 2

        a = s[:half_length]
        b = s[half_length:]

        # Initialize vowel counters
        vowels_in_a = 0
        vowels_in_b = 0

        # Count vowels in the first half
        for char in a:
            if char in vowels:
                vowels_in_a += 1

        # Count vowels in the second half
        for char in b:
            if char in vowels:
                vowels_in_b += 1

        # Return True if both halves have the same number of vowels
        return vowels_in_a == vowels_in_b