> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/345-reverse-vowels-of-a-string.md).

# 345 Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string.

**Example 1:**\
Given s = "hello", return "holle".

**Example 2:**\
Given s = "leetcode", return "leotcede".

**Note:**\
The vowels does not include the letter "y".

The Idea:

* Swap the first and back occurances respectively.

```cpp
string reverseVowels(string s) {

    unordered_set<char> vowels = { 'a', 'e', 'i', 'o', 'u',
                                 'A', 'E', 'I', 'O', 'U'};

    int iter_front = 0;
    int iter_back = s.size() - 1;


    while (iter_front < iter_back) {

        while (iter_front < iter_back && vowels.find(s[iter_front]) == vowels.end())
            iter_front++;
        while (iter_front < iter_back && vowels.find(s[iter_back]) == vowels.end())
            iter_back--;

        swap(s[iter_front], s[iter_back]);
        iter_front++;
        iter_back--;
    }

    return s;
}


int main() {

    string s = "hello";
    cout << reverseVowels(s) << endl;

    return 0;
}
```

**Python**

```python
class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """

        vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
        s = list(s)
        left, right = 0, len(s) - 1

        while left < right:
            while left < right and s[left] not in vowels:
                left += 1

            while left < right and s[right] not in vowels:
                right -= 1

            if s[left] in vowels and s[right] in vowels:
                s[left], s[right] = s[right], s[left]
                left += 1
                right -= 1
            else:
                break

        return ''.join(s)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/345-reverse-vowels-of-a-string.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
