python 2 pointer solution


  • 0
    A
    class Solution(object):
        def reverseVowels(self, s):
            """
            :type s: str
            :rtype: str
            """
            s = list(s)
            vowels = 'aeiouAEIOU'
            
            first = 0
            last = len(s) - 1
            
            done = False
            
            while not done:
                while first <= last and s[first] not in vowels:
                    first += 1
                
                while last >= first and s[last] not in vowels:
                    last -= 1
                
                if last < first:
                    done = True
                else:
                    temp = s[first]
                    s[first] = s[last]
                    s[last] = temp
                
                first += 1
                last -= 1
                
            return ''.join(s)              
    

Log in to reply
 

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