-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
46 lines (38 loc) · 1.19 KB
/
Solution.java
File metadata and controls
46 lines (38 loc) · 1.19 KB
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
44
45
46
public class Solution {
public String reverseVowels(String s) {
if (s == null)
return null;
char[] chars = s.toCharArray();
int left = 0;
int right = s.length() - 1;
while (left < right) {
while (left < right && !isVowel(chars[left]))
left++;
while (left < right && !isVowel(chars[right]))
right--;
char tmp = chars[left];
chars[left] = chars[right];
chars[right] = tmp;
left++;
right--;
}
return String.valueOf(chars);
}
private boolean isVowel(char c) {
switch (c) {
case 'a': // fall through
case 'A': // fall through
case 'e': // fall through
case 'E': // fall through
case 'i': // fall through
case 'I': // fall through
case 'o': // fall through
case 'O': // fall through
case 'u': // fall through
case 'U':
return true;
default:
return false;
}
}
}