Skip to main content

Command Palette

Search for a command to run...

Reverse Vowels of a String

Published
1 min read
Reverse Vowels of a String
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem - 345

class Solution {
    // Method to reverse the vowels in a given string
    public String reverseVowels(String s) {
        // Convert the input string to a character array
        char[] stringArray = s.toCharArray();

        // ArrayList to store the vowels in the original order
        ArrayList<Character> tempVowels = new ArrayList<>();

        // Iterate through each character in the string and add vowels to tempVowels
        for (char leftChar : stringArray) {
            if (leftChar=='a' || leftChar=='e' || leftChar=='i'|| leftChar=='o'|| leftChar=='u' ||
                leftChar=='A' || leftChar=='E' || leftChar=='I'|| leftChar=='O'|| leftChar=='U') {
                tempVowels.add(leftChar);
            }
        }

        // Counter variable to iterate through tempVowels in reverse order
        int z = 0;
        // Iterate through each character in the string in reverse order
        for (int j=stringArray.length-1; j>=0; j--){
            char rightChar = stringArray[j];
            // If the character is a vowel, replace it with the corresponding vowel from tempVowels
            if (rightChar=='a' || rightChar=='e' || rightChar=='i'|| rightChar=='o'|| rightChar=='u' ||
                rightChar=='A' || rightChar=='E' || rightChar=='I'|| rightChar=='O'|| rightChar=='U'){
                stringArray[j] = tempVowels.get(z);
                z++;
            }
        }

        // Convert the character array back to a string and return
        return new String(stringArray);
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.