Reverse Vowels of a String Solution
In this tutorial, we will solve the Reverse Vowels of a String problem using efficient string manipulation techniques. We will provide the implementation of the solution in JavaScript, TypeScript, Python, Java, and C++.
Problem Descriptionβ
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, more than once.
Examplesβ
Example 1:
Input: s = "hello"
Output: "holle"
Example 2:
Input: s = "leetcode"
Output: "leotcede"
Constraintsβ
1 <= s.length <= 3 * 10^5
s
consists of printable ASCII characters.
Solution for Reverse Vowels of a Stringβ
Intuition and Approachβ
To solve this problem, we can use a two-pointer approach to efficiently reverse the vowels in the string. We will place one pointer at the beginning (left
) and another at the end (right
) of the string. We will move these pointers towards each other, swapping vowels when both pointers point to a vowel.
- Two Pointers
Approach: Two Pointersβ
We can use two pointers to efficiently reverse the vowels in the string. This involves traversing the string from both ends and swapping vowels when encountered.
Implementationβ
function reverseVowels(s) { const vowels = new Set('aeiouAEIOU'); const arr = s.split(''); let left = 0; let right = arr.length - 1; while (left < right) { if (!vowels.has(arr[left])) { left++; continue; } if (!vowels.has(arr[right])) { right--; continue; } [arr[left], arr[right]] = [arr[right], arr[left]]; left++; right--; } return arr.join(''); } const input = "hello"; const result = reverseVowels(input); return ( <div> <p> <b>Input:</b> {input} </p> <p> <b>Output:</b> {result} </p> </div> );
Codes in Different Languagesβ
- JavaScript
- TypeScript
- Python
- Java
- C++
function reverseVowels(s) {
const vowels = new Set('aeiouAEIOU');
const arr = s.split('');
let left = 0;
let right = arr.length - 1;
while (left < right) {
if (!vowels.has(arr[left])) {
left++;
continue;
}
if (!vowels.has(arr[right])) {
right--;
continue;
}
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
right--;
}
return arr.join('');
}
function reverseVowels(s: string): string {
const vowels = new Set('aeiouAEIOU');
const arr = s.split('');
let left = 0;
let right = arr.length - 1;
while (left < right) {
if (!vowels.has(arr[left])) {
left++;
continue;
}
if (!vowels.has(arr[right])) {
right--;
continue;
}
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
right--;
}
return arr.join('');
}
def reverseVowels(s: str) -> str:
vowels = set('aeiouAEIOU')
s = list(s)
left, right = 0, len(s) - 1
while left < right:
if s[left] not in vowels:
left += 1
continue
if s[right] not in vowels:
right -= 1
continue
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
return ''.join(s)
class Solution {
public String reverseVowels(String s) {
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
char[] arr = s.toCharArray();
int left = 0, right = arr.length - 1;
while (left < right) {
if (!vowels.contains(arr[left])) {
left++;
continue;
}
if (!vowels.contains(arr[right])) {
right--;
continue;
}
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
return new String(arr);
}
}
class Solution {
public:
string reverseVowels(string s) {
unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
int left = 0, right = s.size() - 1;
while (left < right) {
if (vowels.find(s[left]) == vowels.end()) {
left++;
continue;
}
if (vowels.find(s[right]) == vowels.end()) {
right--;
continue;
}
swap(s[left], s[right]);
left++;
right--;
}
return s;
}
};
Complexity Analysisβ
-
Time Complexity: , where
n
is the length of the string. -
Space Complexity: , as we are not using any additional space.
-
The time complexity is linear in terms of the length of the string. Each character is checked and potentially swapped once, leading to a time complexity of .
-
The space complexity is constant because we only use a few extra variables regardless of the string size.
This solution efficiently reverses the vowels in the string while maintaining the order of non-vowel characters.
Video Explanation of Reverse Vowels of a Stringβ
- English
- Hindi
- JavaScript
- Python
- Java