Find the longest substring containing vowels in even counts

December 10, 2022

array-and-hashmap

Problem URL: Find the longest substring containing vowels in even counts

We will use a sliding window to solve this problem. We will keep track of the number of occurrences of each vowel in the window. We will then check if the number of occurrences of each vowel is even. If it is, we will update the answer.

class Solution:
    def findTheLongestSubstring(self, s: str) -> int:
        lookup = collections.defaultdict(lambda: sys.maxsize)
        cur = (0, 0, 0, 0, 0)                            
        res = lookup[cur] = -1                                
        vowel = {'a': 0, 'e': 1, 'i': 2, 'o': 3, 'u': 4}

        for i, c in enumerate(s):
            if c in vowel:
                idx = vowel[c]
                cur = cur[:idx] + (1-cur[idx],) + cur[idx+1:]

            if lookup[cur] == sys.maxsize: 
                lookup[cur] = i

            res = max(res, i - lookup[cur])

        return res

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