编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
输入: "hello"
输出: "holle"
示例 2:
输入: "leetcode"
输出: "leotcede"
说明:
元音字母不包含字母"y"。
//双指针
class Solution {
private final static Set<Character> set=new HashSet(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
public String reverseVowels(String s) {
int i=0;
int j=s.length()-1;
char []res=new char [j+1];
while(i<=j){
char chi=s.charAt(i);
char chj=s.charAt(j);
if(!set.contains(chi)){
res[i++]=chi;
}else if(!set.contains(chj)){
res[j--]=chj;
}else {
res[i++]=chj;
res[j--]=chi;
}
}
return new String(res);
}
}