forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path345_reverseVowels.java
40 lines (37 loc) · 1.04 KB
/
345_reverseVowels.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
public class Solution {
public String reverseVowels(String s) {
if(s == null || s.length() <= 1) return s;
int i = 0;
int j = s.length()-1;
char[] arr = s.toCharArray();
while(i < j){
while(i < j && !isVowel(arr[i])){ // dont forget i < j in it!!!
i++;
}
while(i < j && !isVowel(arr[j])){ // dont forget i < j in it!!!
j--;
}
if(i >= j) break; // dont forget check i >= j!!!
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
return new String(arr);
}
public boolean isVowel(char c){
HashSet<Character> set = new HashSet<Character>();
set.add('a');
set.add('A');
set.add('e');
set.add('E');
set.add('i');
set.add('I');
set.add('o');
set.add('O');
set.add('u');
set.add('U');
return set.contains(c);
}
}