Skip to main content

Command Palette

Search for a command to run...

Sort Vowels in a String

Published
2 min read
Sort Vowels in 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 0-indexed string s, permute s to get a new string t such that:

  • All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i].

  • The vowels must be sorted in the nondecreasing order of their ASCII values. More formally, for pairs of indices i, j with 0 <= i < j < s.length such that s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than t[j].

Return the resulting string.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.

LeetCode Problem - 2785

class Solution {
    // Method to sort vowels in a given string
    public String sortVowels(String s) {
        String vowels = "aeiouAEIOU"; // Define the vowels

        List<String> al = new ArrayList<>(); // List to store vowels in order of appearance
        // Iterate through each character in the string
        for(char c : s.toCharArray()){
            String currentChar = String.valueOf(c);
            // Check if the current character is a vowel
            if(vowels.contains(currentChar))
                al.add(currentChar); // Add the vowel to the list
        }

        Collections.sort(al); // Sort the list of vowels in alphabetical order

        StringBuilder sb = new StringBuilder(); // StringBuilder to construct the result
        int idx = 0; // Index to iterate through sorted vowels list
        // Iterate through each character in the original string again
        for(char c : s.toCharArray()){
            String currentChar = String.valueOf(c);
            // Check if the current character is a vowel
            if(vowels.contains(currentChar)){
                sb.append(al.get(idx)); // Append the sorted vowel from the list
                idx++; // Move to the next vowel in the sorted list
            } else {
                sb.append(c); // Append the non-vowel character as is
            }
        }
        return sb.toString(); // Return the sorted string with vowels
    }
}

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.