Skip to content

Commit 68c1cb0

Browse files
committed
feat: java code for leetcode-345
1 parent 4a6e09e commit 68c1cb0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Reverse vowels of a string.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public String reverseVowels(String s) {
3+
HashSet<Character> vowels = new HashSet<>(Arrays.asList('a','e','i','o','u','A','E','I','O','U'));
4+
char[] arr = s.toCharArray();
5+
int left=0, right=arr.length-1;
6+
7+
while(left < right){
8+
while(left < right && !vowels.contains(arr[left])) left++;
9+
while(left < right && !vowels.contains(arr[right])) right--;
10+
11+
char temp = arr[left];
12+
arr[left] = arr[right];
13+
arr[right] = temp;
14+
15+
left++ ;
16+
right-- ;
17+
}
18+
return new String(arr);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)