You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.

Leetcode - Reverse Vowels of a String Solution

Beeze Aal
Beeze Aal

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)