Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Simpler algorithm with one loop, and set

Vinicius

Oct 3, 2024

class Solution: def reverseVowels(self, s: str) -> str: vowels = set('aeiouAEIOU') s_list = list(s) left, right = 0, len(s) -1 while left < right: if s_list[left] not in vowels: left += 1 elif s_list[right] not in vowels: right -= 1 else: s_list[left], s_list[right] = s_list[right], s_list[left] left += 1 right -= 1 return ''.join(s_list)

1

0

Comments
Comments