344 Reverse String/ 345 Reverse Vowel String
resource: http://javahungry.blogspot.com/2014/12/5-ways-to-reverse-string-in-java-with-example.html
2 things to remember:
- StringBuilder class has reverse() built in, while String doesn't.
- String has toCharArray(), while StringBuilder doesnt'
Convert String into Char Array then do other twist:
Method1:
Scan through the char Array from end to beginning.
Method2:
- Scan through both side simultaneously.
- Swap the beginning and end.
Method3:
- Store the value into a LinkedList
- use Java.Collection.reverse() function
Use StringBuilder reverse() function
345
Store vowel characters:
resource: http://m.blog.csdn.net/article/details?id=51546410
Into hashSet:
ps: The reason use Set instead of array
http://javahungry.blogspot.com/2014/12/5-ways-to-reverse-string-in-java-with-example.html
Just store them all into a string
But need to be taken care by using
String vowels = "aeiouAEIOU";
vowels.contains(String.valueOf(character));
Using stack to pop and push
SMART ASS :D
code:
public String reverseVowels(String s) {
String vowels = "aeiouAEIOU";
Stack<String> stack = new Stack<String>();
char[] array = s.toCharArray();
for(char ch : array){
if(vowels.contains(String.valueOf(ch))){
stack.add(String.valueOf(ch));
}
}
StringBuilder builder = new StringBuilder();
for(char ch : array)
{
if(vowels.contains(String.valueOf(ch))){
String pop = stack.pop();
builder.append(pop);
}
else{
builder.append(ch);
}
}
return builder.toString();
}