---
# System prepended metadata

title: '[LeetCode 75] DAY 5 - Reverse Vowels of a String'
tags: [LeetCode 75, String]

---

# [LeetCode 75] DAY 5 - 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.

## 解題思考
- 遍歷 `s` 儲存原字串所有字母(aeiou)放入 `arr`
- 遍歷 `s` 當遇到字母時 放入 `arr` 的最後一個元素(`pop()`)

## 程式碼
```javascript
function reverseVowels(s) {
    const vowels = ['a', 'e', 'i', 'o', 'u'];
    let arr = [];
    let result = s.split('');

    for (let i = 0; i < s.length; i++) {
        if (vowels.includes(s[i])) {
            arr.push(s[i])
        }
    }

    for (let i = 0; i < s.length; i++) {
        if (vowels.includes(s[i])) {
            result[i] = arr.pop()
        }
    }

    return result.join('');
};
```

## 延伸思考
- 字串的不可變性
- Two Pointers

## 相關資源
- [345. Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/description/)