## Algorithm

Problem Name: 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.

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
#define IS_VOWEL(C) ((C) == 'a' || (C) == 'A' || \
(C) == 'e' || (C) == 'E' || \
(C) == 'i' || (C) == 'I' || \
(C) == 'o' || (C) == 'O' || \
(C) == 'u' || (C) == 'U')

char* reverseVowels(char* s) {
char *a, *b;

if (!s || !*s) return s;

a = s;
b = &s[strlen(s) - 1];

while (a  <  b) {
if (IS_VOWEL(*a) && IS_VOWEL(*b)) {
// swap
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
a ++;
b --;
} else if (IS_VOWEL(*a)) {
b --;
} else if (IS_VOWEL(*b)) {
a ++;
} else {
a ++;
b --;
}
}
return s;
}
``````
Copy The Code &

Input

cmd
s = "hello"

Output

cmd
"holle"

### #2 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public String reverseVowels(String s) {
int left = 0;
int right = s.length() - 1;
String vowels = "aeiouAEIOU";
char[] chars = s.toCharArray();
while (left  <  right) {
int vowelIndexLeft = vowels.indexOf(chars[left]);
int vowelIndexRight = vowels.indexOf(chars[right]);
if (vowelIndexLeft != -1 && vowelIndexRight != -1) {
char temp = chars[left];
chars[left++] = chars[right];
chars[right--] = temp;
}
else if (vowelIndexRight == -1) {
right--;
}
else {
left++;
}
}
return String.valueOf(chars);
}
}
``````
Copy The Code &

Input

cmd
s = "hello"

Output

cmd
"holle"

### #3 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const reverseVowels = function(s) {
if(s == null || s === '') return ''
const arr = s.split('')
let p = 0
const len = s.length
let e = s.length - 1
const v = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
while(p < e) {
while(v.indexOf(arr[p]) === -1 && p  <  e) p++
while(v.indexOf(arr[e]) === -1 && p < e) e--
const tmp = arr[p]
arr[p] = arr[e]
arr[e] = tmp
p++
e--
}
return arr.join(''>
};
``````
Copy The Code &

Input

cmd
s = "leetcode"

Output

cmd
"leotcede"

### #4 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def reverseVowels(self, s):
r = [c for c in s if c in "aeiouAEIOU"]
return "".join(c in "aeiouAEIOU" and r.pop() or c for c in s)
``````
Copy The Code &

Input

cmd
s = "leetcode"

Output

cmd
"leotcede"

### #5 Code Example with C# Programming

```Code - C# Programming```

``````
using System.Collections.Generic;
using System.Text;

namespace LeetCode
{
public class _0345_ReverseVowelsOfAString
{
public string ReverseVowels(string s)
{
var vowels = new HashSet < char>() { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };

int i = 0, j = s.Length - 1;

var sb = new StringBuilder(s);
while (i  <  j)
{
while (i < s.Length && !vowels.Contains(sb[i]))
i++;
while (j >= 0 && !vowels.Contains(sb[j]))
j--;

if (i == s.Length) break;
if (j == -1) break;

if (i >= j) break;

var temp = sb[i];
sb[i] = sb[j];
sb[j] = temp;

i++;
j--;
}

return sb.ToString();
}
}
}
``````
Copy The Code &

Input

cmd
s = "hello"

Output

cmd
"holle"