-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
32 lines (31 loc) · 1.14 KB
/
Solution.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
class Solution {
public String reverseVowels(String s) {
// DO NOT neglect the uppercase situation!
List<Integer> vowels = new ArrayList<>(); // store the indices of all the vowels
for(int i = 0; i < s.length(); i++) {
char current = s.charAt(i);
if(isVowel(current))
vowels.add(i);
}
String res = "";
int pointer = vowels.size() - 1; // the initial position of pointer should be the last element of vowels
for(int i = 0; i < s.length(); i++) {
char current = s.charAt(i);
if(isVowel(current)) {
res += s.charAt(vowels.get(pointer));
pointer--;
} else {
res += current;
}
}
return res;
}
// return whether the input character is a vowel (uppercase or lowercase) or not
public boolean isVowel(char chrt) {
if(chrt >= 'A' && chrt <= 'Z') // uppercase letter
chrt = (char)(chrt + 32);
if(chrt == 'a' || chrt == 'e' || chrt == 'i' || chrt == 'o' || chrt == 'u')
return true;
return false;
}
}