Back to course home
0% completed
Vote For New Content
JS Solution using a stack
Amar Gill
Oct 23, 2024
An O(n) solution using stack, less complexity than two pointers.
class Solution { isVowel(s) { return ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'].includes(s); } reverseVowels(s) { const stack = []; for (const c of s) { if (this.isVowel(c)) { stack.push(c); } } let newS = ''; for (let i = 0; i < s.length; i++) { const c = s.at(i); if (this.isVowel(c)) { const pop = stack.pop(); newS += pop } else { newS += c } } return newS; } }
0
0
Comments
Comments