-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseVowelsOfString.java
More file actions
29 lines (28 loc) · 876 Bytes
/
Copy pathreverseVowelsOfString.java
File metadata and controls
29 lines (28 loc) · 876 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
class Solution {
public boolean isVowel(char ch){
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U';
}
public String reverseVowels(String s) {
if(s.length()<=1){
return s;
}
char[] chars = s.toCharArray();
int i=0;
int j= s.length() - 1;
while(i<j){
if(isVowel(chars[i]) && isVowel(chars[j])){
// swap vowels
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
i++;
j--;
} else if(!isVowel(chars[i])){
i++;
} else if(!isVowel(chars[j])){
j--;
}
}
return new String(chars);
}
}