import java.util.Arrays;
import java.util.HashSet;
public class ReverseVowels {
//定义一个集合用来存放元音字母
private final static HashSet<Character> vowels=new HashSet<>(Arrays.asList('a','e','i','o','u','A','E','I','O','U'));
public static String reverseVowels(String s){
int pL=0;//左指针
int pR=s.length()-1;//右指针
char[] result=new char[s.length()];//创建新数组
while(pL<=pR){
if(!vowels.contains(s.charAt(pL))){//判断左指针指向元素是否为元音字母
result[pL]=s.charAt(pL);//如果不是向新数组对应下标赋值该元素,左指针右移
pL++;
}
else if(!vowels.contains(s.charAt(pR))){//判断右指针指向元素是否为元音字母
result[pR]=s.charAt(pR);//如果不是向新数组对应下标赋值该元素,右指针左移
pR--;
}
else{//当左右指针都指向元音字母,向新数组pR下标赋值为s字符串pL下标的元音字母
//新数组pL下标赋值为s字符串pR下标的元音字母,来实现元音反转调换
result[pR]=s.charAt(pL);
result[pL]=s.charAt(pR);
pL++;
pR--;
}
}
return new String(result);//最后将新数组作为String参数,返回新的字符串
}
public static void main(String[] args){
String s="abceo";//运行结果:obcea
System.out.println(reverseVowels(s));
}
}