### LeetCode Challenge #345. Reverse Vowels of a String

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 = "hello"
Output: "holle"
```

Example 2:

```Input: s = "leetcode"
Output: "leotcede"
```

Constraints:

• `1 <= s.length <= 3 * 105`
• `s` consist of printable ASCII characters.
##### Video Solution
###### C++ Solution
```				```
class Solution {
public:
string reverseVowels(string s) {

if(s.length()&lt;2)return s;

int i=0;
int j=s.length()-1;
string vowels=&quot;AEIOUaeiou&quot;;

while(i&lt;j)
{
while(i&lt;j and (vowels.find(s[i])==string::npos))i++;

while(j&gt;i and (vowels.find(s[j])==string::npos))j--;

if(i&lt;j)
{
swap(s[i],s[j]);
}
i++;
j--;
}

return s;

}
};
```
```
###### Code Explanation

The purpose of this program is to reverse the positions of the vowels in a given string while keeping the consonants and non-alphabet characters unchanged. Let’s see the approach towards this problem :
1. First of all we checked, whether the length of string is 2 or not. If it does not have then immediately returns the string ‘s’ as it is .(S.length()&lt;2)return s) ;

2. Now if the string is having 2 elements then we initialise ‘i’ at the first position ( i=0 ) and ‘j’ at the last position ( j = n -1 ) And we initializes a string names ‘vowels’ containing all uppercase and lowercase vowels. ( vowels=“aeiouAEIOU”) this is the string to check if a character in the input string is a vowel.

3. The core of the function is a while loop ( i&lt;j and (vowels.find(s[i]== string::npos)) is used to find vowel from left side of ‘ i ‘ And we will increase ‘i ‘until it has achieved any vowel from the left side of the string .
And decrease ‘ j ‘ until it finds vowel from the right side of the string .
(‘ i&lt;j ‘ ) it means there is atleast one vowel on the left side ‘i’ and one on the right side of ‘j’. In this case the code swaps the vowels positions by calling ‘swap(s[ i ] , s[ j ] )’

4. After swapping, just increase ‘ i ‘ by ( i++) decrease ( j – -) The loop continues until ‘i’ is incremented and ‘j’ is decremented to continue searching for other pairs of vowels.
And it will run until ‘ i ‘ is no longer less than ‘j’ which means all the possible pairs or vowels have been swapped.

5. finally we will come outside the loop and the string ‘ s’ is returned .