-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseVowels.java
More file actions
34 lines (23 loc) · 897 Bytes
/
Copy pathReverseVowels.java
File metadata and controls
34 lines (23 loc) · 897 Bytes
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
package LeetcodePractice;
import java.util.Set;
public class ReverseVowels {
public static String reverseVowels(String s) {
Set<Character> vowels = Set.of('a','e','i','o','u','A','E','I','O','U');
char[] chars = s.toCharArray();
int left = 0, right = chars.length - 1;
while (left < right) {
while (left < right && !vowels.contains(chars[left])) left++;
while (left < right && !vowels.contains(chars[right])) right--;
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return new String(chars);
}
public static void main(String[] args) {
System.out.println(reverseVowels("IceCreAm"));
System.out.println(reverseVowels("leetcode"));
}
}