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

0% completed

Vote For New Content
Nabeel Keblawi
Used a stack in python

Nabeel Keblawi

Sep 4, 2024

Alternative solution to two pointers is using a stack, which is a Last In First Out data structure. Since the last vowel in the original string will be the first vowel in the new string, I chose to use a stack. Here's how I did it:

class Solution: def reverseVowels(self, s: str) -> str: vowels = 'aeiouAEIOU' vowelStack = [] # Create an empty stack newString = '' for c in s: if c in vowels: vowelStack.append(c) # Push vowel onto stack for c in s: if c in vowels: newString += vowelStack.pop() # This is where vowels are reversed else: newString += c return newString

1

0

Comments
Comments