-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseVowels.java
More file actions
35 lines (32 loc) · 1.08 KB
/
ReverseVowels.java
File metadata and controls
35 lines (32 loc) · 1.08 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
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class ReverseVowels {
public static void main(String[] args) {
String s1 = "IceCreAm";
String s2 = "leetcode";
System.out.println("Reversed vowels string for " + s1 + " is " + reverseVowels(s1));
System.out.println("Reversed vowels string for " + s2 + " is " + reverseVowels(s2));
}
public static String reverseVowels(String s) {
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
char[] chars = s.toCharArray();
int left = 0;
int right = chars.length - 1;
while (left <= right) {
if (!vowels.contains(chars[left])) {
left++;
}
else if (!vowels.contains(chars[right])) {
right--;
}
else {
chars[left] = chars[right];
chars[right] = chars[left];
left++;
right--;
}
}
return new String(chars);
}
}