Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 11. 4. 23:45

https://leetcode.com/problems/reverse-vowels-of-a-string/

 

Reverse Vowels of a String - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

생각보다는 어려운 문제였음.. 난이도에 속음 =_=

class Solution:        
    def reverseVowels(self, s: str) -> str:
        def is_vowel(c):
            return c in ['a', 'i', 'e', 'o', 'u', 'A', 'I', 'E', 'O', 'U']
        
        start, end = 0, len(s) - 1
        res = list(s)
        
        while start < end:
            while start < len(res) and not is_vowel(res[start]):
                start += 1
            
            while end >= 0 and not is_vowel(res[end]):
                end -= 1
                
            if start < end:
                temp = res[start]
                res[start] = res[end]
                res[end] = temp                
                start += 1
                end -= 1
        
        return ''.join(res)
반응형