Fast and Easy to understand Python solution (72 ms)


  • 0
    A

    This solution is not short by any means. However, it is faster than 97% of submissions (72 ms) as of 06/28/2017 2:49 PM EST!

    class Solution(object):
        def reverseVowels(self, s):
            """
            :type s: str
            :rtype: str
            """
            if len(s) <= 1:
                return(s)
            vowels = {"i", "a", "e", "o", "u", "I", "A", "E", "O", "U"} 
            a = list(s) 
            low, high = 0, len(a) - 1
            while low <= high:
                while (not a[high] in vowels) & (high > low):
                    high -= 1
                while (not a[low] in vowels) and (high > low):
                    low += 1
                if low < high:
                    a[low], a[high] = a[high], a[low]
                high -= 1
                low += 1
            return("".join(a))
    

Log in to reply
 

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