Python two pointers solution


  • 1
    O
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        is_vowel = lambda x: x.lower() in ['a', 'e', 'i', 'o', 'u']
        start, end = 0, len(s) - 1
        s = list(s)
        while start < end:
            if not is_vowel(s[start]):
                start += 1
                continue
            if not is_vowel(s[end]):
                end -= 1
                continue
                
            s[start], s[end] = s[end], s[start]
            start += 1
            end -= 1
        return ''.join(s)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.