Length of Last Word

Easy
215.

Length of Last Word

Length of Last Word is an easy coding problem on strings, asked in Amazon, Microsoft, TCS and Infosys interviews.

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

A word is a maximal run of non-space characters.

Example 1

Input
s = "Hello World"
Output
5

Example 2

Input
s = " fly me to the moon "
Output
4
Explanation
The last word is moon, and the trailing spaces are ignored.

Example 3

Input
s = "a"
Output
1

Constraints

  • 1 <= s.length <= 60
  • s consists of English letters and spaces.
  • There is at least one word in s.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function(s) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Length of Last Word — Solution in Python, Java & C++

Length of Last Word solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on strings.

Length of Last Word is an easy coding problem on strings, asked in Amazon, Microsoft, TCS and Infosys interviews.

  • Difficulty: Easy
  • Topics: Strings
  • Asked at: Amazon, Microsoft, TCS, Infosys, Accenture
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

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

A word is a maximal run of non-space characters.

Example 1

Input: s = "Hello World"
Output: 5

Example 2

Input: s = "   fly me   to   the moon  "
Output: 4
Explanation: The last word is moon, and the trailing spaces are ignored.

Example 3

Input: s = "a"
Output: 1

Constraints

  • 1 <= s.length <= 60
  • s consists of English letters and spaces.
  • There is at least one word in s.

Hints for Length of Last Word

  1. Hint 1

    Work from the right rather than splitting the whole string.

  2. Hint 2

    First skip any trailing spaces, then count characters until the next space or the start of the string.

How to solve Length of Last Word

Scan from the right: skip trailing spaces, then count characters until the next space or the start.

Approach

  1. Set i to the last index and move left while s[i] is a space.
  2. Count characters while i >= 0 and s[i] is not a space.
  3. Return the count.

Why it works

The last word is the final run of non-space characters, and walking backwards reaches it without touching the rest of the string.

Complexity

  • Time — O(n) worst case, typically O(length of last word)
  • Space — O(1)

Pitfalls

  • Trailing spaces are the trap — "hello " has a last word of length 5.
  • split() and taking the last piece works but allocates every word.

Reference solution

Python

def lengthOfLastWord(s: str) -> int:
    i = len(s) - 1
    while i >= 0 and s[i] == " ":
        i -= 1
    length = 0
    while i >= 0 and s[i] != " ":
        length += 1
        i -= 1
    return length

Java

public static int lengthOfLastWord(String s) {
    int i = s.length() - 1;
    while (i >= 0 && s.charAt(i) == ' ') i--;
    int len = 0;
    while (i >= 0 && s.charAt(i) != ' ') {
        len++;
        i--;
    }
    return len;
}

C++

int lengthOfLastWord(string s) {
    int i = (int) s.size() - 1;
    while (i >= 0 && s[i] == ' ') i--;
    int len = 0;
    while (i >= 0 && s[i] != ' ') {
        len++;
        i--;
    }
    return len;
}

JavaScript

var lengthOfLastWord = function(s) {
    let i = s.length - 1;
    while (i >= 0 && s[i] === " ") i--;
    let len = 0;
    while (i >= 0 && s[i] !== " ") {
        len++;
        i--;
    }
    return len;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings