Advertisement

Reverse Words in a String - LeetCode 151 Solution

Reverse Words in a String - Complete Solution Guide

Reverse Words in a String is LeetCode problem 151, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. Example 1: Input: s = "the sky is blue" Output: "blue

Detailed Explanation

The problem requires you to reverse the order of words in a given string. The input string may contain leading or trailing spaces, or multiple spaces between words. The output string must contain the words in reversed order, separated by a single space, and must not have any leading or trailing spaces.

Solution Approach

The provided solutions follow a common three-step approach: splitting the input string into individual words, reversing the order of these words, and then joining them back into a single string with a single space as a separator. The `split()` method automatically handles leading, trailing and multiple spaces between words.

Step-by-Step Algorithm

  1. Step 1: Split the input string into a list (or array) of words using spaces as delimiters. The `split()` method is used for this purpose in Python, Java, and C++. In C, manual parsing is implemented to extract words.
  2. Step 2: Reverse the order of the elements in the list (or array). Python uses slicing `[::-1]`, Java uses `Collections.reverse()`, and C++ uses `std::reverse()`.
  3. Step 3: Join the reversed list of words back into a single string, using a single space as a separator between words. Python uses `' '.join()`, Java uses `String.join()`, C++ manually builds the string with spaces. The C solution also manually builds the string.
  4. Step 4: (C only) Handle memory allocation and deallocation carefully to prevent memory leaks. The C solution requires explicit memory management for the array of words and the resulting string, including error handling for allocation failures.

Key Insights

  • Insight 1: Splitting the string into words is a crucial first step. Using the built-in `split()` method handles multiple spaces correctly.
  • Insight 2: Reversing the order of the words, which can be done using built-in functions or custom loops.
  • Insight 3: Joining the reversed words back into a single string with a single space between each word.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Two Pointers, String.

Companies

Asked at: Accenture, Capgemini, Cisco, Deloitte, HPE, Infosys, Intel, Morgan Stanley, Paytm, Snap, TikTok, Visa, Walmart Labs, Wayfair, Yelp, Zoho, Zopsmart, tcs.