Python Reverse Vowels of a String


  • 0
    Z

    1.Select all the vowels into separate list v and their indexes into new list v_i

    1. Reverse the vowels list v
    2. Replace the letters form original list s by the letters form reversed list v but with the same indexes v_i

    class Solution(object):
    def reverseVowels(self, s):
    """
    :type s: str
    :rtype: str
    """
    s=list(s)
    v=[]
    v_i=[]
    for i in range(len(s)):
    if s[i] in "aeiouAEIOU":
    v.append(s[i])
    v_i.append(i)
    if(len(v)>1):
    v=self.reverse(v)
    for i in range(len(v)):
    s[v_i[i]]=v[i]
    return ''.join(s)
    def reverse(self, s):
    s=list(s)
    for i in range(len(s)//2):
    a=s[i]
    s[i]=s[len(s)-1-i]
    s[len(s)-1-i]=a
    return ''.join(s)


Log in to reply
 

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