Length of Last Word
Given a string s, return the length of the last word.
Note: You may not use any sort of split() method.
Example(s)β
Example 1:
Input: s = "The Daily Byte"
Output: 4
Explanation:
The last word is "Byte" which has 4 characters.
Example 2:
Input: s = "Hello World"
Output: 5
Explanation:
The last word is "World" which has 5 characters.
Example 3:
Input: s = " fly me to the moon "
Output: 4
Explanation:
After trimming trailing spaces, the last word is "moon" which has 4 characters.
Example 4:
Input: s = "luffy is still joyboy"
Output: 6
Explanation:
The last word is "joyboy" which has 6 characters.
Solutionβ
The solution uses reverse traversal:
- Start from the end: Traverse the string from right to left
- Skip trailing spaces: Ignore spaces at the end
- Count characters: Count characters until we hit a space or beginning
- Return length: Return the count
- JavaScript Solution
- Python Solution
JavaScript Solution - Reverse Traversal
/**
* Find length of last word
* @param {string} s - Input string
* @return {number} - Length of last word
*/
function lengthOfLastWord(s) {
let length = 0;
let i = s.length - 1;
// Skip trailing spaces
while (i >= 0 && s[i] === ' ') {
i--;
}
// Count characters of last word
while (i >= 0 && s[i] !== ' ') {
length++;
i--;
}
return length;
}
// Test cases
console.log('Example 1:', lengthOfLastWord("The Daily Byte")); // 4
console.log('Example 2:', lengthOfLastWord("Hello World")); // 5
console.log('Example 3:', lengthOfLastWord(" fly me to the moon ")); // 4
console.log('Example 4:', lengthOfLastWord("luffy is still joyboy")); // 6
console.log('Test 5:', lengthOfLastWord("a")); // 1
console.log('Test 6:', lengthOfLastWord(" ")); // 0Output:
Click "Run Code" to execute the code and see the results.
Python Solution - Reverse Traversal
def length_of_last_word(s: str) -> int:
"""
Find length of last word
Args:
s: Input string
Returns:
int: Length of last word
"""
length = 0
i = len(s) - 1
# Skip trailing spaces
while i >= 0 and s[i] == ' ':
i -= 1
# Count characters of last word
while i >= 0 and s[i] != ' ':
length += 1
i -= 1
return length
# Test cases
print('Example 1:', length_of_last_word("The Daily Byte")) # 4
print('Example 2:', length_of_last_word("Hello World")) # 5
print('Example 3:', length_of_last_word(" fly me to the moon ")) # 4
print('Example 4:', length_of_last_word("luffy is still joyboy")) # 6
print('Test 5:', length_of_last_word("a")) # 1
print('Test 6:', length_of_last_word(" ")) # 0Loading Python runtime...
Output:
Click "Run Code" to execute the code and see the results.
Alternative Solution (More Explicit)β
Here's a more explicit version:
- JavaScript Alternative
- Python Alternative
/**
* More explicit version
*/
function lengthOfLastWordExplicit(s) {
// Trim trailing spaces first
let end = s.length - 1;
while (end >= 0 && s[end] === ' ') {
end--;
}
// Find start of last word
let start = end;
while (start >= 0 && s[start] !== ' ') {
start--;
}
// Length is end - start
return end - start;
}
def length_of_last_word_explicit(s: str) -> int:
"""
More explicit version
"""
# Trim trailing spaces first
end = len(s) - 1
while end >= 0 and s[end] == ' ':
end -= 1
# Find start of last word
start = end
while start >= 0 and s[start] != ' ':
start -= 1
# Length is end - start
return end - start
Complexityβ
- Time Complexity: O(n) - Where n is the length of the string. In the worst case, we traverse the entire string from right to left.
- Space Complexity: O(1) - We only use a constant amount of extra space.
Approachβ
The solution uses reverse traversal:
- Start from the end: Begin at the last character of the string
- Skip trailing spaces: Move left while encountering spaces
- Count characters: Once we hit a non-space character, count characters until we hit a space or the beginning
- Return count: Return the length of the last word
Key Insightsβ
- Reverse traversal: Start from the end to find the last word efficiently
- Skip trailing spaces: Handle strings with trailing whitespace
- No split needed: Can solve without splitting the string
- O(1) space: Only uses a few variables
- Single pass: Traverses string once from right to left
Step-by-Step Exampleβ
Let's trace through Example 1: s = "The Daily Byte"
String: "The Daily Byte"
Index: 012345678901234
T h e D a i l y B y t e
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
Initial: i = 14, length = 0
Step 1: Skip trailing spaces
i = 14: s[14] = 'e' (not space) β stop
i = 14
Step 2: Count characters
i = 14: s[14] = 'e' (not space) β length = 1, i = 13
i = 13: s[13] = 't' (not space) β length = 2, i = 12
i = 12: s[12] = 'y' (not space) β length = 3, i = 11
i = 11: s[11] = 'B' (not space) β length = 4, i = 10
i = 10: s[10] = ' ' (space) β stop
Result: length = 4
Let's trace through Example 3: s = " fly me to the moon "
String: " fly me to the moon "
Index: 012345678901234567890123456
(spaces) f l y m e t o t h e m o o n (spaces)
Initial: i = 25, length = 0
Step 1: Skip trailing spaces
i = 25: ' ' β i = 24
i = 24: ' ' β i = 23
i = 23: 'n' (not space) β stop
i = 23
Step 2: Count characters
i = 23: 'n' β length = 1, i = 22
i = 22: 'o' β length = 2, i = 21
i = 21: 'o' β length = 3, i = 20
i = 20: 'm' β length = 4, i = 19
i = 19: ' ' (space) β stop
Result: length = 4
Visual Representationβ
Example 1: s = "The Daily Byte"
T h e D a i l y B y t e
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
β
Start here
Count backwards: e β t β y β B (4 characters)
Example 3: s = " fly me to the moon "
... m o o n (trailing spaces)
... β
Start here (after skipping spaces)
Count backwards: n β o β o β m (4 characters)
Edge Casesβ
- Trailing spaces: Handled by skipping them first
- Leading spaces: Doesn't matter, we start from the end
- Single word: Works correctly
- Single character: Returns 1
- All spaces: Returns 0 (no word found)
- No spaces: Returns length of entire string
Important Notesβ
- No split(): Problem explicitly forbids using split()
- Trailing spaces: Must handle strings with trailing whitespace
- Word definition: Word is a sequence of non-space characters
- Reverse traversal: More efficient than forward traversal
- O(1) space: Optimal space complexity
Related Problemsβ
- Reverse Words in a String: Different string manipulation
- Valid Palindrome: Check if string is palindrome
- Longest Common Prefix: Find common prefix
- String Compression: Compress string
Takeawaysβ
- Reverse traversal is efficient for finding the last element
- Skip trailing spaces is important for correctness
- No split needed - can solve with simple traversal
- O(1) space is achievable
- Single pass from right to left is optimal