345.Reverse Vowel of a String

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:

Input: "hello"
Output: "holle"

Example 2:

Input: "leetcode"
Output: "leotcede"

Note:
The vowels does not include the letter "y".

class Solution:
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        temp = []
        s = list(s)
        for i in s:
            if i in ['a','e','i','o','u','A','E','I','O','U']:
                temp.append(i)
        temp.reverse()
        pos = 0
        for i in range(len(s)):
            if s[i] in ['a','e','i','o','u','A','E','I','O','U']:
                s[i] = temp[pos]
                pos += 1
        return ''.join(s)

猜你喜欢

转载自www.cnblogs.com/bernieloveslife/p/9771761.html