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


Given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both cases.

Example 1:

Input: s = "hello"
Output: "holle"

Example 2:

Input: s = "leetcode"
Output: "leotcede"


class Solution(object):
def reverseVowels(self, s):
:type s: str
:rtype: str
vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
if s == "":
return ""
s = list(s)
ending = len(s) -1
starting = 0
while starting < ending:
if s[starting] in vowels:
if s[ending] in vowels:
temp = s[starting]
s[starting] = s[ending]
s[ending] = temp
starting += 1
ending -= 1
ending -= 1
elif s[ending] in vowels and s[starting] not in vowels:
starting +=1
starting += 1
ending -= 1
return "".join(s)

Matthew Boyd

Learning, and posting my findings!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store