JS solution


  • 0
    A
    var inArray = function (s, arr) {
        for (let i = 0; i < arr.length; i++) {
            if (s.toLowerCase() == arr[i]) {
                return true
            }
        }
        return false;
    }
    
    /**
     * @param {string} s
     * @return {string}
     */
    var reverseVowels = function (s) {
        const v = ['a', 'e', 'i', 'o', 'u'];
        
        var arr = [];
        for (let i = 0; i < s.length; i++) {
            if (inArray(s[i], v)) {
                arr.push(s[i])
            }
        }
    
        let x=1;
        let res='';
        for (let i = 0; i < s.length; i++) {
            if (inArray(s[i], v)) {
                res += arr[arr.length-x];
                x++;
            } else {
                res += s[i]
            }
        }
    
        return res;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.