Share python code with two pointer


  • 0
    class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        vowels=['a','e','i','o','u','A','E','I','O','U']
        s=list(s)
        slow,fast=0,len(s)-1
        while slow<fast:
            if s[slow] in vowels and s[fast] not in vowels:
                fast-=1
            elif s[slow] not in vowels and s[fast] in vowels:
                slow+=1
            elif s[slow] in vowels and s[fast] in vowels:
                temp=s[slow]
                s[slow]=s[fast]
                s[fast]=temp
                slow+=1
                fast-=1
            else:
                slow+=1
                fast-=1
        return ''.join(s)

Log in to reply
 

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