151. Reverse Words in a String

 

Reversing Words in a String Using JavaScript

Introduction

Reversing the order of words in a string is a common problem in programming, often encountered in coding interviews and competitive programming. It involves manipulating strings and arrays to achieve the desired result efficiently. In this blog post, we'll explore how to reverse the words in a string using JavaScript. We'll break down the approach, understand the logic, and analyze the complexity of our solution.

Problem Statement

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

Intuition

To reverse the order of words in a string, we need to:

  1. Split the string into individual words.

  2. Remove any extra spaces that may exist between the words.

  3. Reverse the order of the words.

  4. Join the words back into a single string.

By breaking the problem down into these steps, we can effectively reverse the order of words in any given string.

Approach

  1. Trim the String:

    • Use the trim() method to remove any leading and trailing whitespace from the string s.

  2. Split into Words:

    • Use the split(" ") method to split the trimmed string into an array of words based on spaces.

  3. Filter Out Empty Strings:

    • Use the filter((e) => e !== '') method to remove any empty strings that may result from consecutive spaces.

  4. Reverse the Array:

    • Use the reverse() method to reverse the order of the elements in the array.

  5. Join Back into a String:

    • Use the join(" ") method to join the elements of the reversed array back into a single string, with each word separated by a space.

Example Code

Here's the complete JavaScript code for reversing the words in a string:

javascript
var reverseWords = function(s) {
    return s.trim()
    .split(" ")
    .filter((e) => e !== '')
    .reverse()
    .join(" ");
};

How It Works

Let's walk through an example to understand how the code works:

javascript
console.log(reverseWords("  hello world  ")); // Output: "world hello"
console.log(reverseWords("a good   example")); // Output: "example good a"
  1. Trim: Removes extra spaces at the beginning and end of the string.

  2. Split: Converts the string into an array of words.

  3. Filter: Removes any empty strings from the array.

  4. Reverse: Reverses the order of the words in the array.

  5. Join: Joins the words back into a single string, with each word separated by a single space.

Complexity Analysis

Time Complexity: O(n)

  • The time complexity is O(n), where n is the length of the input string s. This is because each method (trim(), split(), filter(), reverse(), and join()) processes each character or word once.

Space Complexity: O(n)

  • The space complexity is O(n) because we create an array to hold the words from the input string and then produce a new string as the output, both of which require space proportional to the length of the input string.

Conclusion

This solution efficiently reverses the order of words in a string by leveraging JavaScript's built-in string and array methods. The approach ensures that the solution is both time-efficient and space-efficient, making it suitable for a wide range of input sizes.

Feel free to try out this solution in your projects or coding challenges. If you have any questions or suggestions, feel free to leave a comment. Happy coding!

Leetcode Solution Link : Reverse Words in a String - LeetCode

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

14. Longest Common Prefix