array and hashmap December 1, 2022

Determine if string halves are alike

Time O(n) Space O(1) Open original problem

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)