> 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/186-reverse-words-in-a-string-ii.md).

# 186 Reverse Words in a String II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.

The input string does not contain leading or trailing spaces and the words are always separated by a single space.

For example,\
Given s = "`the sky is blue`",\
return "`blue is sky the`".

Could you do it in-placewithout allocating extra space?

// extra space, O(N)

```cpp
vector<string> tokenize(const string &fullstring)
{
    vector<string> result;
    stringstream ss(fullstring);
    string temp;

    while (getline(ss, temp, ' ')) {
        result.push_back(temp);
    }

    return result;
}

void reverseWords(string &s)
{
    vector<string> tokens = tokenize(s);

    string final_str;
    final_str.reserve(s.size());

    int front = 0, end = tokens.size() - 1;
    while (front < end) {
        swap(tokens.at(front), tokens.at(end));
        front++; end--;
    }

    s.clear();
    for (string &temp : tokens) {
        s += (temp + ' ');
    }
    s.erase(s.end());
}

int main()
{
    string one1 = "";
    reverseWords(one1);
    cout << one1 << endl;

    string one2 = "a";
    reverseWords(one2);
    cout << one2 << endl;

    string one3 = "a b";
    reverseWords(one3);
    cout << one3 << endl;

    string one = "wow look at all these cool words";
    reverseWords(one);
    cout << one << endl;
}
```

// in space, O(N)

The basic idea is to first reverse the string entirely once, and then iterate through the reversed string again to reverse every delimited string.

```cpp
void my_reverse(string &in, int start, int end)
{
    while (start < end) {
        swap(in[start], in[end]);
        start++; end--;
    }
}

void reverseWords(string &s)
{
    reverse(s.begin(), s.end());
    int iter_start = 0;

    for (int i = 0; i < s.length(); i++) {
        if (s[i] == ' ') {
            my_reverse(s, iter_start, i - 1);
            iter_start = i + 1;
        }
    }

    // reverse last word, corner case
    my_reverse(s, iter_start, s.length() - 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://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/186-reverse-words-in-a-string-ii.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.
