Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

Reverse Vowels (easy)

Problem Statement

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= "AEIOU"
Output: "UOIEA"

Example 3:

Input: s= "DesignGUrus"
Output: "DusUgnGires"

Constraints:

  • 1 <= s.length <= 3 * 10<sup>5</sup>
  • s consist of printable ASCII characters.

Try it yourself

Try solving this question here:

.....

.....

.....

Like the course? Get enrolled and start learning!
Amar Gill

Amar Gill

· 2 years ago

An O(n) solution using stack, less complexity than two pointers.

class Solution { isVowel(s) { return ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'].includes(s); } reverseVowels(s) { const stack = []; for (const c of s) { if (this.isVowel(c)) { stack.push(c); } } let newS = ''; for (let i = 0; i < s.length; i++) { const c = s.at(i); if (this.isVowel(c)) { const pop = stack.pop(); newS += pop } else { newS += c } } return newS; } }
Show 1 reply
T

Thai Minh

· 3 years ago

class Solution:   def reverseVowels(self, s: str) -> str:     # TODO: Write your code here     vowels_arr = ['a', 'e', 'u', 'o', 'i']     # front and back pointers     # travel until they are both vowel then swap     f_ptr, b_ptr = 0, len(s) - 1     str_list = list(s)     while f_ptr < b_ptr:       while str_list[f_ptr].lower() not in vowels_arr and f_ptr < b_ptr:         f_ptr += 1       while str_list[b_ptr].lower() not in vowels_arr and b_ptr > f_ptr:         b_ptr -= 1       str_list[f_ptr], str_list[b_ptr] = str_list[b_ptr], str_list[f_ptr]       f_ptr += 1       b_ptr -= 1     return "".join(str_list)
Jose Medina

Jose Medina

· 3 years ago

class Solution {   reverseVowels(s) {     // TODO: Write your code here     let pointerOne = s.length - 1;     let pointerTwo = 0;     let stringArray = Array.from(s);     let hashMap = {       "a": 1,       "e": 1,       "i": 1,       "o": 1,       "u": 1     }     while (pointerOne >= pointerTwo) {       let currentChar = stringArray[pointerOne].toLowerCase()       if (hashMap[currentChar]) {         let testChar = stringArray[pointerTwo].toLowerCase()         if ( hashMap[testChar] ) {           let holdChar = stringArray[pointerTwo];           stringArray[pointerTwo] = stringArray[pointerOne];           stringArray[pointerOne] = holdChar;           pointerOne--;           pointerTwo++;         } else {           pointerTwo++         }       } else {         pointerOne
Zachary Nelson

Zachary Nelson

· 2 years ago

class Solution { reverseVowels(s) { const vowels = new Set('aeiou'.split('')); s = s.split(''); let l = 0; let r = s.length - 1; while (l < r) { if (vowels.has(s[l].toLowerCase()) && vowels.has(s[r].toLowerCase())) { [s[l], s[r]] = [s[r], s[l]]; l++ r-- } if (!vowels.has(s[l].toLowerCase())) l++ if (!vowels.has(s[r].toLowerCase())) r-- } return s.join(''); } }
David Alejandro Delosreyes Ostos

David Alejandro Delosreyes Ostos

· 5 months ago

// 2 pointer method can be applied, with an unique while loop: public class Solution { public String reverseVowels(String s) { int left = 0; int right = s.length() - 1; String vocales = "aeiouAEIOU"; char [] palabra = s.toCharArray(); while (left < right){ if (vocales.indexOf(palabra[left]) == -1){ left ++; }else if(vocales.indexOf(palabra[right]) == -1){ right--; }else{ char temp = palabra[left]; palabra[left] = palabra[right]; palabra[right] = temp; left ++; right --; } } return new String(palabra); } }
W

wave_motion_games

· 3 months ago

using System; public class Solution { static readonly string vowels = "aeiouAEIOU"; public string reverseVowels(string s) { int start = 0; int end = s.Length - 1; var sArray = s.ToCharArray(); while (start < end) { var startIsVowel = isCharVowel(sArray[start]); var endIsVowel = isCharVowel(sArray[end]); if (startIsVowel && endIsVowel) { var temp = sArray[start]; sArray[start] = sArray[end]; sArray[end] = temp; start++; end--; } if (!startIsVowel) { start++; } if (!endIsVowel) { end--; } } return new string(sArray); } private bool isCharVowel(char v) { return vowels.Contains(v); } }