11/11/2020

[LeetCode] 345. Reverse Vowels of a String

 Problem : https://leetcode.com/problems/reverse-vowels-of-a-string/


class Solution:
    def reverseVowels(self, s: str) -> str:
        vowels = set(['a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'])
        result = [w for w in s]
        
        i, j = 0, len(s)-1
        
        while i <= j:
            if result[i] not in vowels:
                i += 1
            elif result[j] not in vowels:
                j -= 1
            else:
                result[i], result[j] = s[j], s[i]
                i += 1
                j -= 1
           
        return "".join(result)

No comments:

Post a Comment