# Leetcode Reverse Vowels of a String problem solution

In this Leetcode Reverse Vowels of a String problem solution, you have 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 cases.

## Problem solution in Python.

```def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""

vowels = "AaEeIiOoUu"

i = 0
j = len(s) - 1

l = list(s)

while i < j:

if l[i] in vowels and l[j] in vowels:
l[i], l[j] = l[j], l[i]
i += 1
j -= 1
elif l[i] in vowels:
j -= 1
elif l[j] in vowels:
i += 1
else:
i += 1
j -= 1

return "".join(l)
```

## Problem solution in Java.

```public String reverseVowels(String s) {
int i = 0, j = s.length() - 1;
char[] sarr = s.toCharArray();
while(i < j) {
if(!s.substring(i, i+1).matches("[aeiouAEIOU]")) {
i++;
} else if(!s.substring(j, j+1).matches("[aeiouAEIOU]")) {
j--;
} else {
char temp = sarr[i];
sarr[i] = sarr[j];
sarr[j] = temp;
i++;
j--;
}
}
return new String(sarr);
}
```

## Problem solution in C++.

```class Solution {
public:
bool isVowel(char x){
return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x== 'u'
|| x == 'A' || x == 'E' || x == 'I' || x == 'O' || x== 'U')?true:false;
}
string reverseVowels(string str) {
int s=0;
int e=str.length()-1;
while(s<=e){
if(!isVowel(str[s]) && isVowel(str[e])){
s++;
}
if(!isVowel(str[e]) && isVowel(str[s])){
e--;
}
if(isVowel(str[s]) && isVowel(str[e])){
swap(str[s],str[e]);
s++;
e--;
}
if(!isVowel(str[s]) && !isVowel(str[e])){
s++;
e--;
}
}
return str;
}
};
```

## Problem solution in C.

```unsigned char isVowels(char c)
{
switch(c)
{
case 'a':
case 'o':
case 'e':
case 'i':
case 'u':
case 'A':
case 'O':
case 'E':
case 'I':
case 'U':
return 1;
default:
return 0;
}
return 0;
}

void swap(char* c1,char* c2)
{
char tmp = 0;
tmp = *c1;
*c1 = *c2;
*c2 = tmp;
}

char* reverseVowels(char* s) {
size_t i = 0, j = 0;

if (NULL == s)
{
return s;
}

for (i = 0, j = strlen(s); i != j; ++i)
{
if (isVowels(*(s+i)))
{
while(i != --j)
{
if (isVowels(*(s+j)))
{
swap(s+i,s+j);
break;
}
}
if (i == j)
{
break;
}
}
}
return s;
}
```