-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
44 lines (39 loc) · 1.09 KB
/
Solution.java
File metadata and controls
44 lines (39 loc) · 1.09 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
package leetCode_345;
import java.util.Arrays;
import java.util.HashSet;
/**
* @author dimdark
* @date 2017-09-12
* @time 9:15 PM
*/
public class Solution {
private HashSet<Character> createVowelSet() {
HashSet<Character> vowels = new HashSet<Character>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
return vowels;
}
public String reverseVowels(String s) {
if (s == null) return null;
HashSet<Character> vowels = createVowelSet();
char[] chars = s.toCharArray();
int i = 0, j = chars.length - 1;
while (i < j) {
while (i < j && !vowels.contains(Character.toLowerCase(chars[i]))) {
i++;
}
while (i < j && !vowels.contains(Character.toLowerCase(chars[j]))) {
j--;
}
if (i >= j) break;
char tmpChar = chars[i];
chars[i] = chars[j];
chars[j] = tmpChar;
i++; j--;
}
return String.valueOf(chars);
}
}