shlogg · Early preview
Rakesh Reddy Peddamallu @rakesh678219

Reverse Vowels In String With Three Approaches

Three approaches to reverse vowels in a string: Naive, Regex, and Two Pointers. The Naive approach uses two loops, the Regex method uses `match` and `replace`, while the Two Pointers approach swaps vowels from start and end of string.

Naive Approach

/**
 * @param {string} s
 * @return {string}
 */
var reverseVowels = function(s) {
    const myStack = [];
    const vowels = ["a","e","i","o","u","A",'E',"I","O","U"];
    const res = []
    for(let i=0;i<s.length;i++){
        if(vowels.includes(s[i])){
            myStack.push(s[i])
            res.push("*")
        }else{
            res.push(s[i])
        }
    }
    for(let i=0;i<s.length;i++){
        if(res[i]=="*"){
            res[i] = myStack.pop()
        }
    }
    return res.join('')
};

    
    

    
    




Using Regex

const reverseVowels = function(s) {...