2785. Sort Vowels in a String LeetCode Solution

In this guide, you will get 2785. Sort Vowels in a String LeetCode Solution with the best time and space complexity. The solution to Sort Vowels in a String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Sort Vowels in a String solution in C++
  4. Sort Vowels in a String solution in Java
  5. Sort Vowels in a String solution in Python
  6. Additional Resources
2785. Sort Vowels in a String LeetCode Solution image

Problem Statement of Sort Vowels in a String

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.

Example 1:

Input: s = “lEetcOde”
Output: “lEOtcede”
Explanation: ‘E’, ‘O’, and ‘e’ are the vowels in s; ‘l’, ‘t’, ‘c’, and ‘d’ are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.

Example 2:

Input: s = “lYmpH”
Output: “lYmpH”
Explanation: There are no vowels in s (all characters in s are consonants), so we return “lYmpH”.

Constraints:

1 <= s.length <= 105
s consists only of letters of the English alphabet in uppercase and lowercase.

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(n)

2785. Sort Vowels in a String LeetCode Solution in C++

class Solution {
 public:
  string sortVowels(string s) {
    string ans;
    vector<char> vowels;

    for (const char c : s)
      if (isVowel(c))
        vowels.push_back(c);

    ranges::sort(vowels);

    int i = 0;  // vowels' index
    for (const char c : s)
      ans += isVowel(c) ? vowels[i++] : c;

    return ans;
  }

 private:
  bool isVowel(char c) {
    static constexpr string_view kVowels = "aeiouAEIOU";
    return kVowels.find(c) != string_view::npos;
  }
};
/* code provided by PROGIEZ */

2785. Sort Vowels in a String LeetCode Solution in Java

class Solution {
  public String sortVowels(String s) {
    StringBuilder sb = new StringBuilder();
    List<Character> vowels = new ArrayList<>();

    for (final char c : s.toCharArray())
      if (isVowel(c))
        vowels.add(c);

    Collections.sort(vowels);

    int i = 0; // vowels' index
    for (final char c : s.toCharArray())
      sb.append(isVowel(c) ? vowels.get(i++) : c);

    return sb.toString();
  }

  private boolean isVowel(char c) {
    return "aeiouAEIOU".indexOf(c) != -1;
  }
}
// code provided by PROGIEZ

2785. Sort Vowels in a String LeetCode Solution in Python

class Solution:
  def sortVowels(self, s: str) -> str:
    kVowels = 'aeiouAEIOU'
    ans = []
    vowels = sorted([c for c in s if c in kVowels])

    i = 0  # vowels' index
    for c in s:
      if c in kVowels:
        ans.append(vowels[i])
        i += 1
      else:
        ans.append(c)

    return ''.join(ans)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.