Title Description
write a function as an input string, the string inversion of vowels.
Example 1:
Enter: "hello"
Output: "holle"
Example 2:
Enter: "leetcode"
Output: "leotcede"
Code
This question since that code is written in a fairly standard, integrated use of several simple data structure, summed up for future reference:
public class Solution {
public String reverseVowels(String s){
List<Integer> indexList = new ArrayList<>();
List<Character> charList = new ArrayList<>();
for(int i=0;i<s.length();i++){
if(isVowel(s.charAt(i))){
indexList.add(i);
charList.add(s.charAt(i));
}
}
int p = 0;
int q = charList.size()-1;
char[] sArr = s.toCharArray();
while(p<indexList.size()){
int index = indexList.get(p);
sArr[index]= charList.get(q);
p++;
q--;
}
StringBuilder sb = new StringBuilder();
for(char ch:sArr){
sb.append(ch);
}
String res = sb.toString();
return res;
}
public boolean isVowel(char c){
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U'){
return true;
}
return false;
}
}
Performance