Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0345 - Reverse Vowels of a String
    0345 - Reverse Vowels of a String
    0345 - Reverse Vowels of a String
    0345 - Reverse Vowels of a String

    0345 - Reverse Vowels of a String

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/reverse-vowels-of-a-string/

    Solution 1 - Collect and Reverse Vowels

    Collect all vowels from the string into a list, reverse the list, then iterate through the original string again replacing each vowel with the next vowel from the reversed list.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(n)SpaceComplexity: O(n)SpaceComplexity:O(n)

    Solution 2 - Two Pointer In-Place Swap

    Use two pointers starting from both ends of the string. Move each pointer inward, skipping non-vowel characters. When both pointers point to vowels, swap them. Continue until the pointers meet.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(n)SpaceComplexity: O(n)SpaceComplexity:O(n)
    """ 0345.1 - Reverse Vowels of a String - Solution 1 - Collect and Reverse Vowels """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def reverseVowels(self, s: str) -> str:
            """Reverse Vowels Function"""
            vowels = set("aeiouAEIOU")
            collected = [c for c in s if c in vowels]
            collected.reverse()
            result = []
            idx = 0
            for c in s:
                if c in vowels:
                    result.append(collected[idx])
                    idx += 1
                else:
                    result.append(c)
            return "".join(result)
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().reverseVowels("IceCreAm"))   # "AceCreIm"
        print(Solution().reverseVowels("leetcode"))    # "leotcede"
        print(Solution().reverseVowels("hello"))       # "holle"
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0345.2 - Reverse Vowels of a String - Solution 2 - Two Pointer In-Place Swap """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def reverseVowels(self, s: str) -> str:
            """Reverse Vowels Function"""
            vowels = set("aeiouAEIOU")
            chars = list(s)
            left, right = 0, len(chars) - 1
    
            while left < right:
                while left < right and chars[left] not in vowels:
                    left += 1
                while left < right and chars[right] not in vowels:
                    right -= 1
                if left < right:
                    chars[left], chars[right] = chars[right], chars[left]
                    left += 1
                    right -= 1
    
            return "".join(chars)
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().reverseVowels("IceCreAm"))   # "AceCreIm"
        print(Solution().reverseVowels("leetcode"))    # "leotcede"
        print(Solution().reverseVowels("hello"))       # "holle"
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()