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!
A

Anonymous

· 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; } }
A

Anonymous

· 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(''); } }
A

Anonymous

· 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
A

Anonymous

· 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)

Reading Progress

0%


Vote for new content