python implementation easy to understand


  • 0
    X
    class Solution(object):
        def reverseVowels(self, s):
            """
            :type s: str
            :rtype: str
            """
            idx = []
            lst = []
            for i in range(len(s)):
                lst.append(s[i])
                if s[i]=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u' or \
                   s[i]=='A' or s[i]=='E' or s[i]=='I' or s[i]=='O' or s[i]=='U':
                    idx.append(i)
            if len(idx)%2 == 0:
                for j in range(len(idx)/2):
                    c = lst[idx[j]]
                    lst[idx[j]] = lst[idx[len(idx)-j-1]]
                    lst[idx[len(idx)-j-1]] = c
            else:
                for j in range((len(idx)-1)/2):
                    c = lst[idx[j]]
                    lst[idx[j]] = lst[idx[len(idx)-j-1]]
                    lst[idx[len(idx)-j-1]] = c
    
            ss = ''.join(lst)
            return ss
    

Log in to reply
 

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