Write a function that takes a string as input and reverse only the vowels of a string.


Example 1:
Given s = "hello", return "holle".



Example 2:
Given s = "leetcode", return "leotcede".



Note:
The vowels does not include the letter "y".

Source: leetcode | Difficulty: EASY
Tags: String algorithms

Amortized analysis

Starter code: class Solution:
    def reverseVowels(self, s: str) -> str:
        

Solutions:
class Solution:

	def reverseVowels(self, s):
		vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
		char_list = list(s)
		(first, last) = (0, len(char_list) - 1)
		while first < last:
			while first < last and char_list[first] not in vowels:
				first += 1
			while first < last and char_list[last] not in vowels:
				last -= 1
			if first >= last:
				break
			(char_list[first], char_list[last]) = (char_list[last], char_list[first])
			first += 1
			last -= 1
		return ''.join(char_list)


class Solution:

	def reverseVowels(self, s):
		vow = set(list('aeiouAEIOU'))
		sList = list(s)
		sPrt = 0
		ePrt = len(sList) - 1
		while sPrt < ePrt:
			if sList[sPrt] in vow and sList[ePrt] in vow:
				(sList[sPrt], sList[ePrt]) = (sList[ePrt], sList[sPrt])
				sPrt = sPrt + 1
				ePrt = ePrt - 1
			elif sList[sPrt] in vow:
				ePrt = ePrt - 1
			elif sList[ePrt] in vow:
				sPrt = sPrt + 1
			else:
				sPrt = sPrt + 1
				ePrt = ePrt - 1
		return ''.join(sList)


class Solution:

	def reverseVowels(self, s):
		if not s or len(s) == 1:
			return s
		vowels = set('aeiouAEIOU')
		s = list(s)
		left = 0
		right = len(s) - 1
		while left < right:
			while left < right and s[left] not in vowels:
				left += 1
			while left < right and s[right] not in vowels:
				right -= 1
			(s[left], s[right]) = (s[right], s[left])
			left += 1
			right -= 1
		return ''.join(s)


class Solution:

	def reverseVowels(self, s):
		if len(s) <= 1:
			return s
		arr = list(s)
		vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
		left = 0
		right = len(s) - 1
		while right > left:
			while left < len(s) and s[left] not in vowels:
				left += 1
			while right > 0 and s[right] not in vowels:
				right -= 1
			if right <= left or right == 0 or left == len(s):
				break
			(arr[left], arr[right]) = (arr[right], arr[left])
			right -= 1
			left += 1
		return ''.join(arr)


class Solution:

	def reverseVowels(self, s):
		vowel = ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')
		(start, end) = (0, len(s) - 1)
		lists = list(s)
		while start < end:
			if lists[start] in vowel:
				while lists[end] not in vowel:
					end -= 1
				if end < start:
					break
				temp = lists[start]
				lists[start] = lists[end]
				lists[end] = temp
				start += 1
				end -= 1
			else:
				start += 1
		return ''.join(lists)


class Solution:

	def reverseVowels(self, s):
		yuanyin = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
		s = list(s)
		i = 0
		j = len(s) - 1
		while i <= j:
			while s[i] not in yuanyin and i < j:
				i += 1
			while s[j] not in yuanyin and i < j:
				j -= 1
			temp = s[i]
			s[i] = s[j]
			s[j] = temp
			i += 1
			j -= 1
		return ''.join(s)


class Solution:

	def reverseVowels(self, s):
		vowels = 'aeiouAEIOU'
		s = list(s)
		l = len(s)
		(p1, p2) = (0, l - 1)
		while p1 < p2:
			if s[p1] in vowels and s[p2] in vowels:
				(s[p1], s[p2]) = (s[p2], s[p1])
				p1 += 1
				p2 -= 1
			elif s[p1] not in vowels:
				p1 += 1
			elif s[p2] not in vowels:
				p2 -= 1
		return ''.join(s)


class Solution:

	def reverseVowels(self, s):
		if not s:
			return s
		vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
		left = 0
		right = len(s) - 1
		res = [s[0]] * len(s)
		while left < right:
			if s[left] in vowels and s[right] in vowels:
				(res[left], res[right]) = (s[right], s[left])
				left += 1
				right -= 1
			else:
				if s[left] not in vowels:
					res[left] = s[left]
					left += 1
				if s[right] not in vowels:
					res[right] = s[right]
					right -= 1
		if left == right:
			res[left] = s[left]
		return ''.join(res)