Reverse Vowels of a String

medium

By - Aman Pareek

Last Updated - 02/09/2024

Problem Statement

You have a string s that consists of both uppercase and lowercase letters. Your task is to reverse just the vowels in this string. The vowels are 'a', 'e', 'i', 'o', and 'u', and their case matters—so 'A' is different from 'a'. While reversing the vowels, the positions of the consonants and the overall structure of the string should stay exactly the same.

Example 1

Input: s = "hello"

Output: "holle"

Solution 1: Two-Pointer Approach

function reverseVowelsTwoPointer(s) {
    const vowels = new Set('aeiouAEIOU');
    const arr = s.split('');
    let left = 0;
    let right = arr.length - 1;

    while (left < right) {
        while (left < right && !vowels.has(arr[left])) left++;
        while (left < right && !vowels.has(arr[right])) right--;
        if (left < right) {
            [arr[left], arr[right]] = [arr[right], arr[left]];
            left++;
            right--;
        }
    }

    return arr.join('');
} 

const s1 = "hello";
reverseVowelsTwoPointer(s1);  //output: holle 

Solution 2: Stack-Based Approach

function reverseVowelsWithStack(s) {
    const vowels = new Set('aeiouAEIOU');
    const stack = [];
    const arr = s.split('');

    for (const char of arr) {
        if (vowels.has(char)) stack.push(char);
    }

    for (let i = 0; i < arr.length; i++) {
        if (vowels.has(arr[i])) {
            arr[i] = stack.pop();
        }
    }

    return arr.join('');
} 

const s1 = "hello";
reverseVowelsWithStack(s1);  //output: holle 

Solution 3: List Comprehension with Reverse

function reverseVowelsListComprehension(s) {
    const vowels = new Set('aeiouAEIOU');
    const arr = s.split('');
    const vowelList = arr.filter(char => vowels.has(char)).reverse();
    let vowelIndex = 0;

    for (let i = 0; i < arr.length; i++) {
        if (vowels.has(arr[i])) {
            arr[i] = vowelList[vowelIndex++];
        }
    }

    return arr.join('');
} 

const s1 = "hello";
reverseVowelsListComprehension(s1);  //output: holle 

Solution 4: Using Array Reverse Method

function reverseVowelsArrayReverse(s) {
    const vowels = new Set('aeiouAEIOU');
    const arr = s.split('');
    const vowelIndices = [];

    for (let i = 0; i < arr.length; i++) {
        if (vowels.has(arr[i])) {
            vowelIndices.push(i);
        }
    }

    let left = 0;
    let right = vowelIndices.length - 1;

    while (left < right) {
        const temp = arr[vowelIndices[left]];
        arr[vowelIndices[left]] = arr[vowelIndices[right]];
        arr[vowelIndices[right]] = temp;
        left++;
        right--;
    }

    return arr.join('');
} 

const s1 = "hello";
reverseVowelsArrayReverse(s1);  //output: holle 

Solution 5: In-Place Replacement with HashSet

function reverseVowelsInPlace(s) {
    const vowels = new Set('aeiouAEIOU');
    const arr = s.split('');
    let i = 0, j = arr.length - 1;

    while (i < j) {
        if (!vowels.has(arr[i])) {
            i++;
        } else if (!vowels.has(arr[j])) {
            j--;
        } else {
            [arr[i], arr[j]] = [arr[j], arr[i]];
            i++;
            j--;
        }
    }

    return arr.join('');
} 

const s1 = "hello";
reverseVowelsInPlace(s1);  //output: holle 

Solution 6: Iterative Replacement with Set

function reverseVowelsIterative(s) {
    const vowels = new Set('aeiouAEIOU');
    const arr = s.split('');
    let vowelChars = [];

    for (const char of arr) {
        if (vowels.has(char)) {
            vowelChars.push(char);
        }
    }

    let index = vowelChars.length - 1;
    for (let i = 0; i < arr.length; i++) {
        if (vowels.has(arr[i])) {
            arr[i] = vowelChars[index--];
        }
    }

    return arr.join('');
} 

const s1 = "hello";
reverseVowelsIterative(s1);  //output: holle 

Solution 7: Using Queue for Vowels

function reverseVowelsWithQueue(s) {
    const vowels = new Set('aeiouAEIOU');
    const queue = [];
    const arr = s.split('');

    for (const char of arr) {
        if (vowels.has(char)) queue.push(char);
    }

    for (let i = 0; i < arr.length; i++) {
        if (vowels.has(arr[i])) {
            arr[i] = queue.pop();
        }
    }

    return arr.join('');
} 

const s1 = "hello";
reverseVowelsWithQueue(s1);  //output: holle 

Solution 8: Using Frequency Count and Replacements

function reverseVowelsFrequency(s) {
    const vowels = new Set('aeiouAEIOU');
    const freq = [];
    const arr = s.split('');

    for (const char of arr) {
        if (vowels.has(char)) freq.push(char);
    }

    freq.reverse();
    let index = 0;

    for (let i = 0; i < arr.length; i++) {
        if (vowels.has(arr[i])) {
            arr[i] = freq[index++];
        }
    }

    return arr.join('');
} 

const s1 = "hello";
reverseVowelsFrequency(s1);  //output: holle 

Solution 9: Using Character Frequency Mapping

function reverseVowelsCharMap(s) {
    const vowels = new Set('aeiouAEIOU');
    const vowelChars = [];
    const arr = s.split('');

    for (const char of arr) {
        if (vowels.has(char)) {
            vowelChars.push(char);
        }
    }

    vowelChars.reverse();
    let charIndex = 0;

    for (let i = 0; i < arr.length; i++) {
        if (vowels.has(arr[i])) {
            arr[i] = vowelChars[charIndex++];
        }
    }

    return arr.join('');
} 

const s1 = "hello";
reverseVowelsCharMap(s1);  //output: holle 

Solution 10: Custom Stack Implementation

function reverseVowelsCustomStack(s) {
    const vowels = new Set('aeiouAEIOU');
    const stack = [];
    const arr = s.split('');

    for (const char of arr) {
        if (vowels.has(char)) {
            stack.push(char);
        }
    }

    return arr.map(char => {
        if (vowels.has(char)) {
            return stack.pop();
        }
        return char;
    }).join('');
} 

const s1 = "hello";
reverseVowelsCustomStack(s1);  //output: holle 

Popular Solutions