Reverse Vowels of a String - Complete Solution Guide
Reverse Vowels of a String is LeetCode problem 345, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a string s , reverse only all the vowels in the string and return it. The vowels are 'a' , 'e' , 'i' , 'o' , and 'u' , and they can appear in both lower and upper cases, more than once. Example 1: Input: s = "IceCreAm" Output: "AceCreIm" Explanation: The vowels in s are ['I', 'e', 'e', 'A'] . On reversing the vowels, s becomes "AceCreIm" . Example 2: Input: s = "leetcode" Output: "leotcede" Constraints: 1 <= s.length <= 3 * 10 5 s consist of printable ASCII characters.
Detailed Explanation
The problem asks you to reverse only the vowels (a, e, i, o, u, and their uppercase counterparts) within a given string while keeping the consonants and their positions unchanged. The input is a string, and the output is a modified string with the vowels reversed. Constraints specify that the input string length is between 1 and 3 * 10<sup>5</sup> characters and consists of printable ASCII characters.
Solution Approach
The provided solutions employ a two-pointer approach. Two pointers, `left` and `right`, start at the beginning and end of the string, respectively. The pointers move towards each other. At each step, they check if the character pointed to is a vowel. If both pointers point to vowels, the vowels are swapped. The process continues until the pointers cross each other. This ensures only vowels are swapped, preserving the order of consonants.
Step-by-Step Algorithm
- Step 1: Initialize two pointers, `left` to 0 and `right` to the last index of the string.
- Step 2: While `left` is less than `right`:
- Step 3: Move `left` pointer to the right until it points to a vowel.
- Step 4: Move `right` pointer to the left until it points to a vowel.
- Step 5: If `left` is still less than `right`, swap the characters at indices `left` and `right`.
- Step 6: Increment `left` and decrement `right`.
- Step 7: Return the modified string.
Key Insights
- Insight 1: Identifying vowels efficiently: A fast way to check if a character is a vowel is crucial for performance. Using a string containing all vowels and checking for membership (e.g., using `in` in Python, `indexOf` in Java, `find` in C++, or `strchr` in C) is efficient.
- Insight 2: Two-pointer approach: Using two pointers, one at the beginning and one at the end of the string, allows for in-place swapping of vowels without needing extra space proportional to the string length. This is significantly more efficient than creating a new string.
- Insight 3: Handling edge cases: The solution needs to handle cases where there are no vowels, only vowels, or a mix of vowels and consonants. The code must correctly manage pointer movements and avoid index out-of-bounds errors.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Two Pointers, String.
Companies
Asked at: Accenture, Accolite, Twitch, USAA, Zoho.