> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Reverse Words in a String Python Solution

> Tested Python solution for LeetCode 151 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 151, Medium. Topics: Two Pointers, String. [View on LeetCode](https://leetcode.com/problems/reverse-words-in-a-string/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 151   # by problem number
lcpy gen -s reverse_words_in_a_string   # by problem name
```

## Problem

Given an input string `s`, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in `s` will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space.

### Examples

```
Input: s = "the sky is blue"
Output: "blue is sky the"
```

```
Input: s = "  hello world  "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
```

```
Input: s = "a good   example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
```

### Constraints

* 1 \<= s.length \<= 10^4
* s contains English letters (upper-case and lower-case), digits, and spaces ' '.
* There is at least one word in s.

**Follow up:** If the string data type is mutable in your language, can you solve it in-place with O(1) extra space?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/reverse_words_in_a_string/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/reverse_words_in_a_string/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n) where n is the length of the string
    # Space: O(n) for the split list
    def reverse_words(self, s: str) -> str:
        # Split by whitespace (handles multiple spaces)
        words = s.split()
        # Reverse the list and join with single space
        return " ".join(reversed(words))
```

## Complexity

| Time                                     | Space                   |
| ---------------------------------------- | ----------------------- |
| O(n) where n is the length of the string | O(n) for the split list |

## Tags

[AlgoMaster 75](/catalog/algo-master-75).
