58. Length of Last Word
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the length of the last word in a given string. A "word" is defined as a continuous sequence of non-space characters. The input string can have leading spaces, trailing spaces, and multiple spaces between words. For example, in " fly me to the moon ", the last word is "moon" and its length is 4.
Solution
Length of Last Word in Python
class Solution:
def lengthOfLastWord(self, s: str) -> int:
length = 0
# Start iterating from the end of the string
i = len(s) - 1
# 1. Skip any trailing spaces
# This loop moves the index 'i' to the left past any spaces
# at the end of the string.
while i >= 0 and s[i] == ' ':
i -= 1
# 2. Count the characters of the last word
# This loop counts characters as long as they are not spaces and
# we are within the string bounds. It stops when a space is found
# or the beginning of the string is reached.
while i >= 0 and s[i] != ' ':
length += 1
i -= 1
return length
Length of Last Word in Java
class Solution {
public int lengthOfLastWord(String s) {
int length = 0;
// Start iterating from the end of the string
int i = s.length() - 1;
// 1. Skip any trailing spaces
// This loop moves the index 'i' to the left past any spaces
// at the end of the string.
while (i >= 0 && s.charAt(i) == ' ') {
i--;
}
// 2. Count the characters of the last word
// This loop counts characters as long as they are not spaces and
// we are within the string bounds. It stops when a space is found
// or the beginning of the string is reached.
while (i >= 0 && s.charAt(i) != ' ') {
length++;
i--;
}
return length;
}
}
Length of Last Word in C++
class Solution {
public:
int lengthOfLastWord(string s) {
int length = 0;
// Start iterating from the end of the string
int i = s.length() - 1;
// 1. Skip any trailing spaces
// This loop moves the index 'i' to the left past any spaces
// at the end of the string.
while (i >= 0 && s[i] == ' ') {
i--;
}
// 2. Count the characters of the last word
// This loop counts characters as long as they are not spaces and
// we are within the string bounds. It stops when a space is found
// or the beginning of the string is reached.
while (i >= 0 && s[i] != ' ') {
length++;
i--;
}
return length;
}
};
Length of Last Word in TypeScript
function lengthOfLastWord(s: string): number {
let length = 0;
// Start iterating from the end of the string
let i = s.length - 1;
// 1. Skip any trailing spaces
// This loop moves the index 'i' to the left past any spaces
// at the end of the string.
while (i >= 0 && s[i] === ' ') {
i--;
}
// 2. Count the characters of the last word
// This loop counts characters as long as they are not spaces and
// we are within the string bounds. It stops when a space is found
// or the beginning of the string is reached.
while (i >= 0 && s[i] !== ' ') {
length++;
i--;
}
return length;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.