Skip to content
LC-0345 Easy LeetCode

345. Reverse Vowels of a String

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 58% Topics: Two Pointers, String
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        vowels = "aeiou"
        string = list(s)
        i, j = 0, len(s) - 1
        while i < j:
            if string[i].lower() not in vowels:
                i += 1
            elif string[j].lower() not in vowels:
                j -= 1
            else:
                string[i], string[j] = string[j], string[i]
                i += 1
                j -= 1
        return "".join(string)

Solution from kamyu104/LeetCode-Solutions · MIT