-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.java
More file actions
26 lines (25 loc) · 781 Bytes
/
18.java
File metadata and controls
26 lines (25 loc) · 781 Bytes
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
// Q18. Reverse Vowels of a String
class Solution {
public String reverseVowels(String s) {
char[] chars = s.toCharArray();
int left = 0;
int right = chars.length - 1;
String vowels = "aeiouAEIOU";
while (left < right) {
while (left < right && vowels.indexOf(chars[left]) == -1) {
left++;
}
while (left < right && vowels.indexOf(chars[right]) == -1) {
right--;
}
if (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
}
return new String(chars);
}
}