-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseVowels.java
45 lines (38 loc) · 1.15 KB
/
ReverseVowels.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
/**
* Reverse Vowels of a String
*
* Write a function that takes a string as input and reverse only the vowels of a string.
*
* e.g. IN: "hello", OUT: "holle"
* IN: "leetcode", OUT: "leotcede"
*
* Note: The vowels does not include the letter "y".
*/
class ReverseVowels {
public static void main(String[] args) {
System.out.println(new ReverseVowels().reverseVowels("hello"));
}
public boolean isVowel(char c) {
c = Character.toLowerCase(c);
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
public String reverseVowels(String s) {
char[] chars = s.toCharArray();
int start = 0, end = chars.length - 1;
while (start < end) {
if (!isVowel(chars[start])) { start++; }
if (!isVowel(chars[end])) { end--; }
if (isVowel(chars[start]) && isVowel(chars[end])) {
swap(chars, start, end);
start++;
end--;
}
}
return new String(chars);
}
public void swap(char[] c, int i, int j) {
char temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}