Reverse Vowels of a String
Reverse the order of the vowels in a string while every other letter stays where it is.
Given a string s
Example 1
Output: "holle"
Explanation: the vowels e and o swap places.
Example 2
Output: "deopnete"
Explanation: the vowels e, e, o, e become e, o, e, e.
The Idea
Think of the word as a row of letter tiles. The consonant tiles are glued down, and only the vowel tiles may be picked up and put back. In "hello""holle"
With two vowels, reversing is just one swap. Example 2 has more. In "deepnote""deopnete"
Reversing any list follows a simple pattern: the first item swaps with the last, the second with the second to last, and so on toward the middle. Here the list is the vowels, so the first vowel swaps with the last vowel, then the second vowel with the second to last. Look at the picture below.
The row labelled "before" is "deepnote""deopnete"
So we need two markers: one that walks in from the left and stops at each vowel, and one that walks in from the right and does the same. Every time both have stopped, their two vowels swap. Two markers like these, moving toward each other from the two ends, are called two pointers.
Solution Approach
Turning that idea into code means settling four things: where to make the changes, how to recognise a vowel, how each pointer finds its next vowel, and when to stop.
Where the letters change
Can we swap two letters inside a Java StringStringchartoCharArray()new String(...)
A simpler way also works: copy all the vowels into a list in one pass, then walk the string a second time and write them back from the end of the list. It takes the same order of time, but needs that extra list and a second pass. The two pointers do the job in one pass with nothing extra.
Recognising a vowel
How do we test one character? The string "aeiouAEIOU"indexOf(ch)chch"IceCreAm"
Finding the next vowel
What does each pointer do between swaps? The left pointer, lohi
Stopping in the middle
When are we done? Once lohi
Java Solution
The method reverseVowels
- First, copy the string into a
arraychar withc , and sets.toCharArray() to 0 andlo to the last position.hi - Repeat the next three steps in an outer loop for as long as
is less thanlo .hi - Inside it, a first inner loop moves
right whilelo is not a vowel. A second inner loop movesc[lo] left whilehi is not a vowel. Both inner loops also stop as soon asc[hi] reacheslo .hi - Swap
andc[lo] through a temporary variablec[hi] .tmp - Move
one place right andlo one place left.hi - When the outer loop ends, return
.new String(c)
The helper isVowel"aeiouAEIOU".indexOf(ch)main
public class ReverseVowelsOfString {
public static String reverseVowels(String s) {
char[] c = s.toCharArray();
int lo = 0;
int hi = c.length - 1;
while (lo < hi) {
while (lo < hi && !isVowel(c[lo])) {
lo++; // find the next vowel from the left
}
while (lo < hi && !isVowel(c[hi])) {
hi--; // and the next vowel from the right
}
char tmp = c[lo];
c[lo] = c[hi];
c[hi] = tmp;
lo++;
hi--;
}
return new String(c);
}
private static boolean isVowel(char ch) {
return "aeiouAEIOU".indexOf(ch) >= 0;
}
public static void main(String[] args) {
System.out.println(reverseVowels("hello"));
System.out.println(reverseVowels("deepnote"));
System.out.println(reverseVowels("IceCreAm"));
System.out.println(reverseVowels("xyz"));
System.out.println(reverseVowels("bab"));
System.out.println("[" + reverseVowels("") + "]");
}
}
Running it prints:
holle
deopnete
AceCreIm
xyz
bab
[]
holledeopnete"IceCreAm"AceCreIm
xyzlohibab[]hi
The lo < hi"hello"hello"xyz"ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3lo
Complexity
The work is one walk of the two pointers toward each other, plus the copy into the array.
- Time: O(n), where n is the number of characters.
only moves right andlo only moves left, so together they take at most about n steps before they meet. Each vowel test looks through a fixed string of 10 letters.hi - Space: O(n), for the
array. A Javachar cannot be changed, soString copies all n characters to have somewhere to swap them.toCharArray()
Without the vowel test, the same two pointers reverse the whole string: see