Lädt...


🔧 LeetCode Challenge: 58. Length of Last Word 🚀


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Top Interview 150

String manipulation is a common coding interview topic, and this problem is a great example of working with substrings efficiently. Let’s solve LeetCode 58: Length of Last Word using a straightforward approach.

🚀 Problem Description

Given a string s consisting of words and spaces, return the length of the last word in the string.

  • A word is defined as a maximal substring consisting of non-space characters only.

💡 Examples

Example 1

Input: s = "Hello World"  
Output: 5  
Explanation: The last word is "World" with length 5.

Example 2

Input: s = "fly me to the moon"  
Output: 4  
Explanation: The last word is "moon" with length 4.

Example 3

Input: s = "luffy is still joyboy"  
Output: 6
Explanation: The last word is "joyboy" with length 6.

🏆 JavaScript Solution

The problem can be solved by trimming unnecessary spaces and locating the last word efficiently.

Implementation

var lengthOfLastWord = function(s) {
    const words = s.trim().split(' ');

    return words[words.length - 1].length;
};

🔍 How It Works

  1. Trim the String:
    • Remove leading and trailing spaces using trim() to simplify the logic.
  2. Split the String:
    • Use split(' ') to divide the string into words based on spaces.
  3. Get the Last Word:
    • Access the last element of the array (words[words.length - 1]) and return its length.

🔑 Complexity Analysis

  • > Time Complexity: O(n) is the length of the string. Trimming and splitting the string both run in linear time.
  • > Space Complexity: O(n), as splitting creates an array of words.

Alternative Solution (No Split)

For improved space efficiency, traverse the string backward to find the last word:

var lengthOfLastWord = function(s) {
    let length = 0;
    let i = s.length - 1;

    // Skip trailing spaces
    while (i >= 0 && s[i] === ' ') {
        i--;
    }

    // Count characters of the last word
    while (i >= 0 && s[i] !== ' ') {
        length++;
        i--;
    }

    return length;
};

🔑 Complexity Analysis (Alternative Solution)

  • > Time Complexity: O(n), as we traverse the string once.
  • > Space Complexity: O(1), as no extra data structures are used.

📋 Dry Run

Input: s = "fly me to the moon"
Length of Last Word
Output: 4

✨ Pro Tips for Interviews

  1. Clarify constraints:

    • Can the string be empty?
    • Are there multiple spaces between words?
  2. Optimize for edge cases:

    • Input with only spaces (" "0).
    • Single-word input ("hello"5).

📚 Learn More

Check out the full explanation and code walkthrough on my Dev.to post:
👉 Length of Last Word - JavaScript Solution

How would you solve this problem? Let’s discuss! 🚀

JavaScript #LeetCode #CodingInterview #ProblemSolving

...

🔧 LeetCode Challenge: 58. Length of Last Word 🚀


📈 48.33 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 58. Length of Last Word 🚀


📈 48.33 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 290. Word Pattern - JavaScript Solution 🚀


📈 29.13 Punkte
🔧 Programmierung

🔧 58. Length of Last Word


📈 27.51 Punkte
🔧 Programmierung

🔧 58. Length of Last Word


📈 27.51 Punkte
🔧 Programmierung

🔧 Automating Your LeetCode Journey: Building an Enterprise-Grade LeetCode to GitHub Sync System


📈 26.08 Punkte
🔧 Programmierung

🔧 Cracking Stack and Queue LeetCode Problems: A Step-by-Step Guide to Solving LeetCode Challenges


📈 26.08 Punkte
🔧 Programmierung

🔧 Maximum Number of Vowels in a Substring of Given Length | LeetCode | Java


📈 25.49 Punkte
🔧 Programmierung

🔧 JavaScript Array Length – How to Find the Length of an Array in JS


📈 24.91 Punkte
🔧 Programmierung

🐧 How to Find Length of Largest Array Dimension in MATLAB Using length() Function?


📈 24.91 Punkte
🐧 Linux Tipps

🔧 LeetCode Meditations: Word Break


📈 21.36 Punkte
🔧 Programmierung

🔧 LeetCode Meditations: Word Search II


📈 21.36 Punkte
🔧 Programmierung

🔧 LeetCode Meditations: Word Search


📈 21.36 Punkte
🔧 Programmierung

🔧 Word Pattern - LeetCode - Java


📈 21.36 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 15. 3Sum - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 27. Remove Element - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge 13: Roman to Integer - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 11. 📦 Container With Most Water - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 88. Merge Sorted Array - JavaScript Solution


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge 42. Trapping Rain Water - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 167. Two Sum II - Input Array Is Sorted - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 Leetcode Blind75 Challenge.


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 134. Gas Station - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 392. Is Subsequence - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 Key JavaScript Concepts I Mastered During LeetCode’s 30-Day Challenge


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 205. Isomorphic Strings - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 238. Product of Array Except Self - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 🧩 LeetCode Challenge: Remove Element | Top Interview Questions [Java Solution]


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 383. Ransom Note - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 238. Product of Array Except Self - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 🧩 LeetCode Challenge: Remove Element | Top Interview Questions [Java Solution]


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge 289: Game of Life - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 380. Insert Delete GetRandom O(1) - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 125. Valid Palindrome - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

🔧 LeetCode Challenge: 73. Set Matrix Zeroes - JavaScript Solution 🚀


📈 20.82 Punkte
🔧 Programmierung

matomo