[LeetCode] Problem 345 - Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string.

Example

No.1

Input: “hello”

Output: “holle”

No.2

Input: “leetcode”

Output: “leotcede”

Note

The vowels does not include the letter “y”.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public String reverseVowels(String s) {
String str = "aeiouAEIOU";
char[] ch = s.toCharArray();
int start = 0;
int end = s.length() - 1;

while (start < end) {
while (start < end && str.indexOf(ch[start]) == -1)
start++;

while (start < end && str.indexOf(ch[end]) == -1)
end--;

char temp = ch[start];
ch[start] = ch[end];
ch[end] = temp;
start++;
end--;
}

return String.valueOf(ch);
}