Leetcode 2785: Sort Vowels in a String

grid47
grid47
Exploring patterns and algorithms
Feb 2, 2024 5 min read

You are given a string s. The task is to permute s such that all consonants remain in their original places, and the vowels are sorted in the nondecreasing order of their ASCII values.
Problem
Approach
Steps
Complexity
Input: The input consists of a string s containing only English letters (uppercase and lowercase).
Example: Input: s = 'ReAcTivE'
Constraints:
• 1 <= s.length <= 10^5
• s consists only of letters of the English alphabet in uppercase and lowercase.
Output: Return the new string where the consonants remain in their original places and the vowels are sorted in non-decreasing ASCII order.
Example: Output: 'ReACTivE'
Constraints:
Goal: Sort the vowels in the string while leaving consonants in place.
Steps:
• Extract the vowels from the string and store them in a list.
• Sort the list of vowels based on their ASCII values.
• Reconstruct the string by replacing the vowels in their original positions with the sorted vowels.
Goal: Constraints on the input size and string content.
Steps:
• 1 <= s.length <= 10^5
• s consists only of English alphabet letters (uppercase and lowercase).
Assumptions:
• The input string may contain both uppercase and lowercase vowels.
Input: Input: 'ReAcTivE'
Explanation: The vowels in 'ReAcTivE' are 'e', 'A', 'i'. After sorting them based on their ASCII values, the new string becomes 'ReACTivE'.

Input: Input: 'hEllOWoRlD'
Explanation: The vowels 'e', 'O', 'O' are sorted, resulting in 'hElLOOWrLD'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus