forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_345.java
35 lines (33 loc) · 1.13 KB
/
_345.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
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class _345 {
public static class Solution1 {
public String reverseVowels(String s) {
StringBuilder sb = new StringBuilder(s);
Set<Character> vowels = new HashSet(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
//use two pointers approach would be the fastest
int i = 0;
int j = s.length() - 1;
while (i < j) {
char left = s.charAt(i);
char right = s.charAt(j);
while (i < j && !vowels.contains(left)) {
i++;
left = s.charAt(i);
}
while (i < j && !vowels.contains(right)) {
j--;
right = s.charAt(j);
}
char temp = left;
sb.setCharAt(i, right);
sb.setCharAt(j, temp);
i++;
j--;
}
return sb.toString();
}
}
}