-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseVowels.java
48 lines (39 loc) · 1.14 KB
/
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
41
42
43
44
45
46
47
48
// 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".
class Solution {
public String reverseVowels(String s) {
HashSet<Character> set = new HashSet<>();
char[] characters = s.toCharArray();
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');
int start = 0;
int end = s.length() - 1;
while(start < end){
while(start < end && !set.contains(characters[start])){
start++;
}
while(start < end && !set.contains(characters[end])){
end--;
}
char temp = characters[start];
characters[start++] = characters[end];
characters[end--] = temp;
}
String str = new String(characters);
return str;}
}