Reverse Vowels of a String

Problem: Reverse Vowels of a String

Use two pointers, one from beginning and one from the end, to scan for vowels. If either one finds one, make it wait and another one continue scanning. If two vowels are found, just replace them.

Code in Python:

class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        l, r = 0, len(s)-1
        vowels = "aeiouAEIOU"
        s = list(s)
        while l < r:
            if s[l] not in vowels: l += 1
            elif s[r] not in vowels: r -= 1
            else:
                s[l], s[r] = s[r], s[l]
                l += 1
                r -= 1
        return ''.join(s)

results matching ""

    No results matching ""