# LeetCode Journal #006: 345. Reverse Vowels of a String | Two Pointers | While loop concept
> Given a string s, reverse only all the vowels in the string and return it.
> The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.
**Example 1:**
Input: s = "IceCreAm"
Output: "AceCreIm"
Explanation:
The vowels in s are ['I', 'e', 'e', 'A']. On reversing the vowels, s becomes "AceCreIm".
**Example 2:**
Input: s = "leetcode"
Output: "leotcede"
## Intuition
Reverse -> Two pointers
Two index start at left and right side.
Check if the char is vowel.
## Approach
1.Intialize a Set to store vowels.
2.Intialize two pointers variables.
3.Transfer string to charArray cause string is immutable.
4..While loop and check, if it's not vowel then keep moving.
5.Exit the nested loop inside the main one if it's a vowel.
6.Exchange each pointer's char if they both are vowels.
## Complexity
### Time complexity:
```
main loop: O(n/2)
nested loop1: O(n)
nested loop2: O(n)
Exchange char: O(1)
=> O(n)
```
### Space complexity:
```
Set: O(1)
Exchange for new CharArray: O(n)
=> O(n)
```
## Code
class Solution {
private final static Set<Character> VOWELS = Set.of('A','E','I','O','U','a','e','i','o','u');
public String reverseVowels(String s) {
int left = 0;
int right = s.length() - 1;
char[] charArray = s.toCharArray();
while (left < right) {
while (left < right && !isVowel(charArray[left])) {
left++;
}
while (left < right && !isVowel(charArray[right])) {
right--;
}
if (left < right) {
char tmp = charArray[right];
charArray[right] = charArray[left];
charArray[left] = tmp;
left++;
right--;
}
}
return new String(charArray);
}
private boolean isVowel(char letter) {
return VOWELS.contains(letter);
}
}
## Takeaway
### Two Pointers
Problems that talk about reversing strings or arrays can probably be solved by using two pointers. These solutions usually start with a while loop with the condition `left < right`.
### While loop
The core concept is "continuing execution" as long as the condition is true.