Problem statement

https://leetcode.com/problems/reverse-vowels-of-a-string/

Solution

Use two pointers approach: one pointer starts from the beginning, another from the end. When we meet vowels, change them and continue.

Complexity

Time and space complexity is O(n).

Code

class Solution:
    def reverseVowels(self, s):
        l, vowels = list(s), set("aeiouAEIOU")
        beg, end = 0, len(s) - 1
        while beg < end:
            while beg < end and l[beg] not in vowels: beg += 1
            while beg < end and l[end] not in vowels: end -= 1
            l[beg], l[end] = l[end], l[beg]
            beg, end = beg + 1, end - 1
            
        return "".join(l)