Reverse Vowels in a String
Write a function that takes a string as input and reverses only the vowels present in it. The function should return the modified string with all non-vowel characters remaining static in their original positions. Vowels are defined as 'a', 'e', 'i', 'o', and 'u', and can appear in both lowercase and uppercase.
[ "hello" ]
Explanation. The vowels 'e' and 'o' have been reversed, while the other characters remain in their original positions.
[ "leetcode" ]
Explanation. The vowels 'e', 'e', 'o', 'e' are reversed to 'o', 'e', 'e', 'e', keeping non-vowel characters fixed.
[ "xyz" ]
Explanation. There are no vowels in the input string, so it remains unchanged.
[ "AEIOU" ]
Explanation. All characters are vowels and they are completely reversed.
[ "" ]
Explanation. Empty string input returns an empty string output.
Follow-up: Can you optimize your solution to use constant space?
The input string will not be null. The string can be empty or contain only non-vowel characters.
- Views
- 4