58. Length of Last Word

 

Calculating the Length of the Last Word in a String Using JavaScript

Introduction

String manipulation is a common task in programming, and one such challenge is finding the length of the last word in a given string. This problem is often encountered in coding interviews and competitive programming. In this blog post, we'll explore an efficient solution using JavaScript. We'll walk through the approach, understand the underlying logic, and analyze the complexity of our solution.

Problem Statement

Given a string s consisting of words and spaces, our goal is to return the length of the last word in the string. A word is defined as a maximal substring consisting of non-space characters only.

Intuition

To solve the problem, we need to handle extra spaces at the beginning and end of the string and accurately identify the last word. The approach leverages string manipulation functions to achieve this efficiently.

Approach

Our approach involves three main steps:

  1. Trim the String:

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

  2. Find the Last Space Index:

    • Use the lastIndexOf(" ") method to find the position of the last space in the string. If there is no space, it returns -1.

  3. Slice and Measure the Last Word:

    • Use s.slice(lastIndex + 1) to extract the substring from the position right after the last space to the end of the string, which gives us the last word.

    • The length property of the resulting substring gives the length of the last word.

LeetCode Solution

Here's the complete JavaScript solution for the problem:

javascript
var lengthOfLastWord = function(s) {
    s = s.trim();
    let lastIndex = s.lastIndexOf(" ");
    return s.slice(lastIndex+1).length;  
};

Example

Let's walk through some examples to understand how the code works:

javascript
console.log(lengthOfLastWord("Hello World")); // Output: 5
console.log(lengthOfLastWord("   fly me   to   the moon  ")); // Output: 4
console.log(lengthOfLastWord("luffy is still joyboy")); // Output: 6
console.log(lengthOfLastWord("   a")); // Output: 1

In these examples, the function accurately trims the input string, finds the last space, and calculates the length of the last word.

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 we perform operations that iterate through the string (e.g., trim(), lastIndexOf(), and slice()).

Space Complexity: O(1)

  • The space complexity is O(1), as we are using a fixed amount of additional space for variables (lastIndex) and the temporary string produced by slice().

Conclusion

This solution efficiently handles the task of finding the length of the last word in a string by leveraging simple yet powerful string manipulation methods in JavaScript. The approach ensures that the solution is both time-efficient and space-efficient, making it suitable for large 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!

For a detailed explanation and to see the complete solution, check out my Length of Last Word - 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

LeetCode Problem: Implement pow(x, n) leetcode:- 50