345. Reverse Vowels of a String

Reverse Vowels of a String

Reverse Vowels of a String


Given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a''e''i''o', and 'u', and they can appear in both lower and upper cases, more than once.

 

Example 1:

Input: s = "hello"
Output: "holle"


Example 2:

Input: s = "leetcode"
Output: "leotcede"

 

Constraints:

  • 1 <= s.length <= 3 * 105
  • s consist of printable ASCII characters.

Approach) Brute Force

/**
 * @param {string} s
 * @return {string}
 */
var reverseVowels = function(s) {
    const vowels = ['a','e','i','o','u'];
    let strArray = s.split("");
    let start = 0;
    let end = strArray.length-1;
    while(start < end){
        if(!vowels.includes(strArray[start].toLowerCase())){
            start++;
            continue;
        }
        if(!vowels.includes(strArray[end].toLowerCase())){
            end--;
            continue;
        }

            let temp = strArray[start];
            strArray[start] = strArray[end];
            strArray[end] = temp;
            start++;
            end--;
    }

    return strArray.join("");
    
};

Approach) Another Brute Force

 var reverseVowels = function (s) {
		let arr = [];
		const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];

		// Loop to detect vowels and store in an array
		for (let i = 0; i < s.trim().length; i++) {
			if (vowels.includes(s[i])) {
				arr.push(s[i]);
				//  s = s.replace(s[i], "~")
			}
		}

		// Convert string vowels from array vowels to descending order
		return s.split("").map(req => vowels.includes(req) ? (req = arr.pop()) : req).join("");
	};

Approach) Two Pointer

const reverseVowels = (s) => {
    const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
    const sArr = s.split('');
    let l = 0;
    let r = s.length - 1;
    
    while(l < r) {
        if(vowels.has(sArr[l]) && vowels.has(sArr[r])) {
            [sArr[l], sArr[r]] = [sArr[r], sArr[l]];
            l += 1;
            r -= 1;
            continue;
        }
        if(!vowels.has(sArr[l])) l += 1;
        if(!vowels.has(sArr[r])) r -= 1;
    }
    
    return sArr.join('');
};

Approach) Javascript - Using Build Methods

const reverseVowels = (s) => {
  const vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"];
  const sorted = [...s].filter((c) => vowels.includes(c));
  return [...s].reduce((a, c) => a + (vowels.includes(c) ? sorted.pop() : c), "" );
};

Conclusion

That’s all folks! In this post, we solved LeetCode problem #345. Reverse Vowels of a String

I hope you have enjoyed this post. Feel free to share your thoughts on this.

You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.


In this blog, I have tried to collect & present the most important points to consider when improving Data structure and logic, feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!

Comments

Popular Post