-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseVowelsOfAString.java
62 lines (48 loc) · 1.22 KB
/
ReverseVowelsOfAString.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package easy._345;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author Larry
*
* Write a function that takes a string as input and reverse only the vowels of a string.
*
* Example 1:
*
* Input: "hello"
* Output: "holle"
* Example 2:
*
* Input: "leetcode"
* Output: "leotcede"
* Note:
* The vowels does not include the letter "y".
*/
public class ReverseVowelsOfAString {
public String reverseVowels(String s) {
Set<Character> set = new HashSet<>();
set.addAll(Arrays.asList('a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'));
char[] chars = s.toCharArray();
int left = 0;
int right = chars.length - 1;
while (left < right) {
if (!set.contains(chars[left])) {
left ++;
continue;
}
if (!set.contains(chars[right])) {
right --;
continue;
}
char tmp = chars[left];
chars[left] = chars[right];
chars[right] = tmp;
left ++;
right --;
}
return String.copyValueOf(chars);
}
}