-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReverseVowelsOfAString.java
50 lines (43 loc) · 1.18 KB
/
ReverseVowelsOfAString.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
49
50
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.HashSet;
import java.util.Set;
// https://leetcode.com/problems/reverse-vowels-of-a-string/
public class ReverseVowelsOfAString {
private final String input;
public ReverseVowelsOfAString(String input) {
this.input = input;
}
public String solution() {
Set<Character> vowels = new HashSet<>(10);
vowels.add('a');
vowels.add('A');
vowels.add('e');
vowels.add('E');
vowels.add('i');
vowels.add('I');
vowels.add('o');
vowels.add('O');
vowels.add('u');
vowels.add('U');
char[] letters = input.toCharArray();
int l = 0;
int r = letters.length - 1;
while (l < r) {
char left = letters[l];
if (!vowels.contains(left)) {
l++;
continue;
}
char right = letters[r];
if (!vowels.contains(right)) {
r--;
continue;
}
letters[l] = right;
letters[r] = left;
l++;
r--;
}
return String.valueOf(letters);
}
}