Given a 0-indexed string s, permute s to get a new string t such that:
i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i].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 <= 105s consists only of letters of the English alphabet in uppercase and lowercase.In #2785 Sort Vowels in a String, the goal is to reorder only the vowels in a string while keeping all consonants and their positions unchanged. A common strategy is to perform the task in two passes. First, iterate through the string and collect all characters that belong to the vowel set (a, e, i, o, u in both lowercase and uppercase). Store these vowels in a separate list.
Next, sort the collected vowels using a standard sorting algorithm. After sorting, traverse the original string again and replace each vowel position with the next element from the sorted list. This ensures that consonants remain untouched while vowels appear in sorted order.
This approach works efficiently because only the vowel subset is sorted rather than the entire string. If n is the string length and k is the number of vowels, the main cost comes from sorting the vowels. The algorithm is straightforward to implement and leverages simple data structures like arrays or lists.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Collect vowels, sort, and replace | O(n + k log k) | O(k) |
Fisher Coder
Use these hints if you're stuck. Try solving on your own first.
Add all the vowels in an array and sort the array.
Replace characters in string s if it's a vowel from the new array.
This approach consists of two main parts: identifying and sorting vowels, and reconstructing the string with sorted vowels while keeping consonants in their original places. In the first pass, we traverse the string to collect all the vowels and simultaneously record their indices. Then we sort the collected vowels. In the second pass, we construct the new string by placing the vowels back in their recorded positions in sorted order, while consonants are directly copied from the original string.
Time Complexity: O(n + m^2), where n is the length of the string and m is the number of vowels (due to sorting, which isn't optimized here to O(m log m)).
Space Complexity: O(n), where n is the length of the string for storing the result and vowels.
1using System;
2using System.Collections.Generic;
3
4class SortVowelsInString {
5 static bool IsVowel(char c) {
6 c = Char.ToLower(c);
7 return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
8 }
9
10 public static string SortVowelsInStringMethod(string s) {
11 List<char> vowels = new List<char>();
12 foreach (char c in s) {
if (IsVowel(c)) {
vowels.Add(c);
}
}
vowels.Sort();
char[] result = s.ToCharArray();
int vowelIndex = 0;
for (int i = 0; i < result.Length; i++) {
if (IsVowel(result[i])) {
result[i] = vowels[vowelIndex++];
}
}
return new string(result);
}
static void Main() {
string test = "lEetcOde";
Console.WriteLine(SortVowelsInStringMethod(test));
}
}This C# approach stores vowels in a List<char> and sorts them with List.Sort(). It uses a character array to efficiently manage in-place character replacements and constructs the final output using the updated array.
This in-place approach is optimized beyond the two-pass method using a two-pointer technique tailored for scenarios where vowels need to be sorted and replaced directly in the original string buffer. Instead of additional space for vowel storage and indexing, we identify vowels and simultaneously allow swapping to form sorted order based on ASCII comparison efficiently.
Time Complexity: O(n^2) for in-place sorting of vowels.
Space Complexity: O(1) due to in-place modification.
class SortVowelsInPlace {
static bool IsVowel(char c) {
c = Char.ToLower(c);
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
public static string SortVowelsInStringMethod(string str) {
char[] s = str.ToCharArray();
int left = 0, right = s.Length - 1;
while (left < right) {
while (left < right && !IsVowel(s[left])) left++;
while (left < right && !IsVowel(s[right])) right--;
if (left < right && s[left] > s[right]) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
}
left++;
right--;
}
return new string(s);
}
static void Main() {
string test = "lEetcOde";
Console.WriteLine(SortVowelsInStringMethod(test));
}
}Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Problems like this are common in technical interviews because they test string manipulation, indexing, and careful iteration. While the exact problem may vary, similar tasks involving selective sorting or character filtering frequently appear in FAANG-style interviews.
A dynamic array or list is typically used to store vowels while scanning the string. A set can also help quickly check whether a character is a vowel. After sorting the list, you iterate again to replace vowel positions in the string.
The optimal approach is to scan the string, extract all vowels into a list, sort them, and then place them back into the original string positions where vowels occur. This keeps consonants fixed while ensuring vowels appear in sorted order. The main cost is sorting the collected vowels.
Yes, only the vowels need to be sorted rather than the entire string. By extracting vowels first and sorting that subset, you reduce unnecessary work. This keeps the algorithm efficient even for long strings with relatively few vowels.
The C# implementation utilizes a two-pointer strategy within a character array, performing in-place swaps when vowel positions pair inversely to the ASCII order, ensuring minimally redundant operations for optimal in-place adjustments.