Determine if string halves are alike

December 1, 2022

array-and-hashmap

Problem URL: Determine if string halves are alike

We will itrerate over the string, for first half we increase the count and for second half we decrease the count. At the end if the count is 0, then the string halves are alike.

class Solution:
    def halvesAreAlike(self, s: str) -> bool:
        count = 0
        for i, ch in enumerate(s):
            if ch.lower() in "aeiou":
                count += 1 if i < len(s)//2 else -1
        return count == 0             

Time complexity: O(n) Space complexity: O(1)