Simple JavaScript Code with O(n) Complexity


  • 0
    P
    /**
     * @param {string} s
     * @return {string}
     */
    var reverseVowels = function(s) {
        var vowels = 'aeiouAEIOU';
        var arr = [];
        var arrReverse = [];
        for (let i = 0; i < s.length; i++) {
            if (vowels.indexOf(s[i]) > -1)
                arr.push(i);
        }
        arrReverse = arr.slice().reverse();
        var len = parseInt(arrReverse.length / 2);
        var result = s.split('');
        for (let i = 0; i < len; i++) {
            let temp = result[arr[i]];
            result[arr[i]] = result[arrReverse[i]];
            result[arrReverse[i]] = temp;
        }
        return result.join('');
    
    };
    
    
    

  • 0
    P

    Hi friends!! can u make this code more better in terms of complexity?


Log in to reply
 

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