관리 메뉴

솜씨좋은장씨

[leetCode] 345. Reverse Vowels of a String (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 345. Reverse Vowels of a String (Python)

솜씨좋은장씨 2020. 8. 7. 21:25
728x90
반응형

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".

 

Solution

class Solution(object):
    def reverseVowels(self, s):
        s_list = list(s)
        
        vowels = []
        
        for i, val in enumerate(s_list):
            if val in ['a', 'o', 'e', 'i', 'u', 'A', 'O', 'E', 'I', 'U']:
                vowels.append((i, val))
                
        for j in range(len(vowels)//2):
            s_list[vowels[j][0]] = vowels[len(vowels)-j-1][1]
            s_list[vowels[len(vowels)-j-1][0]] = vowels[j][1]
            
        return ''.join(s_list)

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments