Reverse Vowels of a String

2016-06-23  本文已影响0人  再见小浣熊

Difficulty: Easy

Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".

    def reverseVowels(self, s):
    """
    :type s: str
    :rtype: str
    """
    vowels = ['a', 'e', 'i', 'o', 'u']
    s = list(s)
    begin = 0
    end = len(s) - 1
    while begin < end:
        while begin < end and s[begin].lower() not in vowels:
            begin += 1
        while begin < end and s[end].lower() not in vowels:
            end -= 1
        s[begin], s[end] = s[end], s[begin]
        begin += 1
        end -= 1
            
    return ''.join(s)
上一篇下一篇

猜你喜欢

热点阅读