Leetcode - Reverse Vowels of a String Solution

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 in python

class Solution:
    def reverseVowels(self, s: str) -> str:
        s = list(s)
        vowels = []
        indices = []
        for index, char in enumerate(s):
            if char.lower() in {"a", "e", "i", "o", "u"}:
                indices.append(index)
                vowels.append(char)
        for index,char in zip(reversed(indices),vowels):
            s[index] = char
        return "".join(s)

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe