-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.17.java
43 lines (32 loc) · 1 KB
/
Solution.17.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*345. 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', and 'u', and they can appear in both lower and upper cases, more than once.
Example 1:
Input: s = "hello"
Output: "holle"
Example 2:
Input: s = "leetcode"
Output: "leotcede"*/
class Solution {
public String reverseVowels(String s) {
char[] word = s.toCharArray();
int start = 0;
int end = s.length() - 1;
String vowels = "aeiouAEIOU";
while (start < end) {
while (start < end && vowels.indexOf(word[start]) == -1) {
start++;
}
while (start < end && vowels.indexOf(word[end]) == -1) {
end--;
}
char temp = word[start];
word[start] = word[end];
word[end] = temp;
start++;
end--;
}
String answer = new String(word);
return answer;
}
}