# 345.Reverse Vowels of a String

**345.Reverse Vowels of a String**

难度:Easy

> 编写一个函数，以字符串作为输入，反转该字符串中的元音字母。

```
示例 1:

输入: "hello"
输出: "holle"
示例 2:

输入: "leetcode"
输出: "leotcede"
说明:
元音字母不包含字母"y"。
```

先用一个数组保存元音字符的下标号，然后遇到元音字符的时候，反转即可。

```
class Solution {
set<char> vowels={'a','e','i','o','u','A','E','I','O','U'};
public:
    string reverseVowels(string s) {
        string res;
        // cout<<s.length()<<endl;
         if(!s.length()) return res;
        vector<int>indexs;
        for(int i=0;i<s.length();i++)
        {
            if(vowels.count(s[i])) indexs.push_back(i);
        }
        if(indexs.empty()) return s;
        // for(auto c:indexs)
        //     cout<<c<<" ";
        int start=0;
        for(int i=0;i<s.length();i++)
        {
            if(start <indexs.size() &&i==indexs[start]   )
            {
                res+=s[indexs[indexs.size()-1-start]];
                ++start;
            //    cout<<res<<endl;
            }
            else 
            {
                res+=s[i];
        //        cout<<res<<" -  "<<endl;
            }
            
        }
        return res;
    }
};
```

> 执行用时 :20ms, 在所有 C++ 提交中击败了54.85%的用户\
> 内存消耗 :11.4MB, 在所有 C++ 提交中击败了5.12%的用户


---

# Agent Instructions: 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:

```
GET https://dfine.gitbook.io/leetcode/345.reverse_vowels_of_a_string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
