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

# 345 Reverse Vowels of a String

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

## 1. Question

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".

## 2. Implementation

**(1) Two Pointers**

```java
class Solution {
    public String reverseVowels(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }

        String vowels = "aeiouAEIOU";

        char[] letters = s.toCharArray();
        int start = 0, end = s.length() - 1;

        while (start < end) {
            while (start < end && vowels.indexOf(letters[start]) < 0) {
                ++start;
            }

            while (start < end && vowels.indexOf(letters[end]) < 0) {
                --end;
            }

            if (start >= end) {
                break;
            }

            swap(letters, start, end);
            ++start;
            --end;
        }
        return new String(letters);
    }

    public void swap(char[] letters, int start, int end) {
        char temp = letters[start];
        letters[start] = letters[end];
        letters[end] = temp;
    }
}
```

## 3. Time & Space Complexity

**Two Pointers:** 时间复杂度O(n), 空间复杂度O(1)


---

# 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://protegejj.gitbook.io/algorithm-practice/leetcode/two-pointers/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.
