Reverse Vowels of a String
Given a string s, reverse only all the vowels in the string and return it.The vowels are 'a', 'e', 'i', 'o', 'u', and they can appear in both lower and upper cases.Other characters should remain in their original positions.
[ "hello" ]
Explanation. The vowels are 'e' and 'o'. Reversing them swaps their positions: 'o' takes 'e''s place, and 'e' takes 'o''s place.
[ "leetcode" ]
Explanation. The vowels are 'e' (at index 1), 'e' (at index 4), 'o' (at index 5), 'e' (at index 7). After reversing only these vowels, 'e' (original index 7) goes to index 1, 'o' (original index 5) goes to index 4, 'e' (original index 4) goes to index 5, and 'e' (original index 1) goes to index 7.
[ "aA" ]
Explanation. The vowels are 'a' and 'A'. Reversing them results in 'A' then 'a'.
[ "aeiou" ]
Explanation. All characters are vowels. Reversing the entire string of vowels results in 'u', 'o', 'i', 'e', 'a'.
[ "rhythm" ]
Explanation. There are no vowels in the string 'rhythm', so the string remains unchanged.
[ "DesignPatterns" ]
Explanation. Vowels are 'e','i','a','e','a','e'. Reversed: 'e','a','e','a','i','e'. The string becomes DesagnPitterNs
Follow-up: Can you solve this problem with O(1) extra space complexity, assuming the input string is mutable (e.g., convertible to a character array)?
1. `1 <= s.length <= 3 * 10^5`2. `s` consists of printable ASCII characters.
- Views
- 3