-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution345.java
More file actions
41 lines (40 loc) · 933 Bytes
/
Solution345.java
File metadata and controls
41 lines (40 loc) · 933 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
30
31
32
33
34
35
36
37
38
39
40
41
package leetcode_algorithms;
public class Solution345{
public static String reverseVowels(String str) {
char[] s = str.toCharArray();
int left = 0;
int right = s.length-left - 1;
while (left < right) {
while(left<right&&!isvowel(s[left])){
left++;
}
while(left<right&&!isvowel(s[right])){
right--;
}
if (left<right) {
char temp;
temp=s[left];
s[left]=s[right];
s[right]=temp;
}
left++;
right--;
}
return new String(s);
}
public static boolean isvowel(char a) {
String vowel = "aeiouAEIOU";
for (int i = 0; i < vowel.length(); i++) {
if (a == vowel.charAt(i)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
String name = "leetcode";
String reverseName = reverseVowels(name);
System.out.println("--------");
System.out.println(reverseName);
}
}