Domain 1EasyCORE

Length of Last Word

The problem requires us to find the length of the last word in a given string `s`. A word is defined as a maximal substring consisting of non-space characters only. The input string can have leading, trailing, or multiple spaces between words. If there are no words in the string (i.e., it's either empty or contains only spaces), we should return 0.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#String

58. Length of Last Word

Model: qwen2.5-coder:14b
Topics: String
FAANG asked: 1
Acceptance rate: 33.5%


Problem Breakdown

The problem requires us to find the length of the last word in a given string s. A word is defined as a maximal substring consisting of non-space characters only. The input string can have leading, trailing, or multiple spaces between words. If there are no words in the string (i.e., it’s either empty or contains only spaces), we should return 0.

Inputs:

  • A single string s containing English letters and spaces.

Outputs:

  • An integer representing the length of the last word in the string.

Implicit Constraints/Edge Cases:

  • The input string can be empty.
  • The input string might contain only spaces.
  • There could be multiple spaces between words.

Intuition

A brute-force approach would involve splitting the string into words using spaces as delimiters and then returning the length of the last word in the resulting list. However, this approach has a time complexity of O(n) due to the split operation and slicing, which might not be optimal for very large strings.

The key insight is to avoid unnecessary operations by iterating through the string from the end to the beginning. This way, we can skip all trailing spaces and directly find the last word without splitting the entire string.

Approach

  1. Initialize a variable length to 0 to keep track of the length of the last word.
  2. Start iterating from the end of the string towards the beginning.
  3. Skip any trailing spaces by decrementing the index until a non-space character is found.
  4. Once a non-space character is encountered, start counting characters until a space or the beginning of the string is reached.
  5. Return the length of the last word.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the string. In the worst case, we might need to iterate through the entire string if there are no spaces.
  • Space Complexity: O(1). We are using a constant amount of extra space regardless of the input size.

Solution

def lengthOfLastWord(s: str) -> int:
    # Initialize length of the last word
    length = 0
    
    # Start from the end of the string and move backwards
    i = len(s) - 1
    
    # Skip trailing spaces
    while i >= 0 and s[i] == ' ':
        i -= 1
    
    # Count the length of the last word
    while i >= 0 and s[i] != ' ':
        length += 1
        i -= 1
    
    return length

Edge Cases Handled

  • Empty String (s = ""): The function returns 0 because there are no words.
  • String with Only Spaces (s = " "): The function returns 0 because there are no words.
  • Single Word Without Trailing Spaces (s = "Hello"): The function correctly returns 5.
  • Multiple Words with Trailing Spaces (s = "Hello World "): The function correctly returns 5, ignoring the trailing spaces.

The solution provided is correct and efficient. It handles all edge cases as described in the problem statement. The time complexity is O(n), which is optimal for this problem, and the space complexity is O(1).