LeetCode 75: Reverse Words in a String – Python Solution Explained

Introduction

The Reverse Words in a String problem is a classic string manipulation challenge that appears frequently in coding interviews and is part of the LeetCode 75 study plan.

Given a string s, reverse the order of the words in the string.

  • A word is defined as a sequence of non-space characters.
  • Words are separated by one or more spaces.
  • You must return a string where:
    • Words are in reverse order,
    • Separated by a single space,
    • And there are no leading or trailing spaces.
Reverse Words in a String

Example 1:

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

Example 2:

Input: s = "hello world"
Output: "world hello"

Constraints:

  • 1 <= s.length <= 10^4
  • s contains English letters (upper/lowercase), digits, and spaces
  • No leading/trailing words, but may contain extra spaces

Python Solution for Reverse Words in a String

class Solution:
    def reverseWords(self, s: str) -> str:
        # Split the string into a list of words
        words = s.split()
        # Reverse the order of words
        reversed_words = words[::-1]
        # Join the reverse words to form the final result
        reversed_string = ' '.join(reversed_words)
        return reversed_string

Step-by-Step Explanation

Step 1: Split the String into Words

words = s.split()
  • The split() method:
    • Automatically removes extra spaces,
    • And splits the string by whitespace.
  • Result is a list of words.

Example:

s = "  the sky  is blue  "
words = ['the', 'sky', 'is', 'blue']

Step 2: Reverse the List of Words

reversed_words = words[::-1]
  • The slicing syntax [::-1] reverses the list in Python.

Output:

['blue', 'is', 'sky', 'the']

Step 3: Join the Words with a Single Space

reversed_string = ' '.join(reversed_words)
  • Combines the reversed words into a single string with one space between each word.

Final Output:

"blue is sky the"

Step 4: Return the Result

return reversed_string
  • The reversed sentence is returned as the output.

Time and Space Complexity

  • Time Complexity:O(n)
    • We scan the entire string once to split, reverse, and join.
  • Space Complexity:O(n)
    • Temporary space for list of words and final string.

This solution The Reverse Words in a String uses Python’s built-in string and list functions to reverse the order of words in a string efficiently.

Conclusion

The Reverse Words in a String problem from LeetCode 75 is a powerful exercise in mastering string manipulation in Python. By focusing on trimming spaces, splitting words correctly, and reversing their order, you gain hands-on experience with core string operation. This problem not only tests your ability to clean and format input but also sharpens your understanding of Python’s built-in methods like .split() and .join(). Mastering this challenge sets a solid foundation for more advanced text-processing problems you’ll encounter in coding interviews and real-world applications.

LeetCode75: Reverse Vowels of a String

2 thoughts on “LeetCode 75: Reverse Words in a String – Python Solution Explained”

Leave a Comment