""" 0345.1 - Reverse Vowels of a String - Solution 1 - Collect and Reverse Vowels """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def reverseVowels(self, s: str) -> str:
"""Reverse Vowels Function"""
vowels = set("aeiouAEIOU")
collected = [c for c in s if c in vowels]
collected.reverse()
result = []
idx = 0
for c in s:
if c in vowels:
result.append(collected[idx])
idx += 1
else:
result.append(c)
return "".join(result)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().reverseVowels("IceCreAm")) # "AceCreIm"
print(Solution().reverseVowels("leetcode")) # "leotcede"
print(Solution().reverseVowels("hello")) # "holle"
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0345.2 - Reverse Vowels of a String - Solution 2 - Two Pointer In-Place Swap """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def reverseVowels(self, s: str) -> str:
"""Reverse Vowels Function"""
vowels = set("aeiouAEIOU")
chars = list(s)
left, right = 0, len(chars) - 1
while left < right:
while left < right and chars[left] not in vowels:
left += 1
while left < right and chars[right] not in vowels:
right -= 1
if left < right:
chars[left], chars[right] = chars[right], chars[left]
left += 1
right -= 1
return "".join(chars)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().reverseVowels("IceCreAm")) # "AceCreIm"
print(Solution().reverseVowels("leetcode")) # "leotcede"
print(Solution().reverseVowels("hello")) # "holle"
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()