Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Reverse Vowels of a String
Luqman Shaban
Luqman Shaban

Posted on

Reverse Vowels of a String

Hey there! Welcome back again. In this subsequent article, we'll go through the process of solving the reverse vowels leetcode challenge. Here is the task we're given:
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.

We are tasked with reversing the vowels in a given string, denoted as's'. We need to check whether the string contains any vowels and then return the string with its vowels reversed. For example, if the string has the value 'hello', it should be transformed to 'holle'. Similarly, if the string has the value 'leetcode', it should be changed to 'leotcede'.

Here's the starter code:

function reverseVowels(s: string): string {};
Enter fullscreen modeExit fullscreen mode

To address this challenge, we'll create two arrays: vowels andreversedVowelString. The vowels array will store any existing vowels of the string s whilereversedVowelString array will store the string itself. we'll then loop over the string and check if any vowel exists in string s, if it does, we store the vowels in the vowels array. The string s will then be stored inreversedVowelString. We'll run another loop that will iterate overreversedVowelString and replace its vowels with the vowels in vowels array in reverse order. Eventually the arrayreversedVowelString will be returned as a string.

function reverseVowels(s: string): string {    let condition = 'AEIOUaeiou';    let vowel: string[] = []    let finalWord: string[] = []    for(let i = 0; i < s.length; i ++) {        if(condition.includes(s[i])) {            vowel.push(s[i])        }        finalWord.push(s[i])    }    for(let i = 0; i < finalWord.length; i ++) {        if(condition.includes(finalWord[i])) {            finalWord[i] = vowel.pop() !;        }    }    return finalWord.join('')};
Enter fullscreen modeExit fullscreen mode

In conclusion, tackling the reverse vowels LeetCode challenge involves carefully reversing the vowels while keeping the non-vowel characters intact. By creating arrays to store both the vowels and the original string characters, we can effectively manipulate the string to meet the challenge requirements.
Happy coding!!

LinkedIn
Twitter
GitHub

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Founder & CEO at wrenify, I specialize in creating custom software solutions that drive innovation and efficiency for small businesses.
  • Location
    Nairobi, Kenya
  • Education
    Institute of software Technologies
  • Work
    Software developer
  • Joined

More fromLuqman Shaban

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp