4.6 KiB
Length of Last Word
Problem Number: 58 Difficulty: Easy Category: String LeetCode Link: https://leetcode.com/problems/length-of-last-word/
Problem Description
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
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.
Constraints:
1 <= s.length <= 10^4sconsists of only English letters and spaces' '- There will be at least one word in
s
My Approach
I used a String Manipulation approach with built-in functions. The key insight is to strip leading and trailing spaces, then split the string by spaces to get the last word.
Algorithm:
- Strip leading and trailing whitespace from the string
- Split the string by spaces to get an array of words
- Return the length of the last word in the array
Solution
The solution uses string manipulation with built-in functions. See the implementation in the solution file.
Key Points:
- Uses strip() to remove leading and trailing spaces
- Uses split() to separate words by spaces
- Returns length of the last word in the resulting array
- Handles multiple spaces between words automatically
- Simple and efficient approach
Time & Space Complexity
Time Complexity: O(n)
- strip() operation: O(n)
- split() operation: O(n)
- length calculation: O(1)
- Total: O(n)
Space Complexity: O(n)
- split() creates a new array of words
- Each word can be up to length n
- Total: O(n)
Key Insights
-
String Manipulation: Using built-in string functions makes the solution simple and readable.
-
Whitespace Handling: strip() automatically handles leading and trailing spaces.
-
Word Separation: split() with default delimiter (space) handles multiple spaces between words.
-
Last Word Access: Using negative indexing (-1) provides easy access to the last element.
-
Edge Case Handling: The approach naturally handles strings with multiple spaces.
-
Built-in Efficiency: Python's built-in string functions are optimized for performance.
Mistakes Made
-
Manual Parsing: Initially might try to manually parse the string character by character.
-
Wrong Split: Not using split() or using wrong delimiter.
-
Missing Strip: Not removing leading/trailing spaces before processing.
-
Complex Logic: Overcomplicating the solution with unnecessary loops.
Related Problems
- Reverse Words in a String (Problem 151): Reverse the order of words in a string
- Valid Palindrome (Problem 125): Check if a string is a palindrome
- Longest Common Prefix (Problem 14): Find common prefix among strings
- Valid Parentheses (Problem 20): Check if parentheses are valid
Alternative Approaches
- Manual Parsing: Iterate from end to find last word - O(n) time, O(1) space
- Regular Expressions: Use regex to find last word - O(n) time complexity
- Two Pointers: Use pointers to find word boundaries - O(n) time, O(1) space
Common Pitfalls
- Manual Parsing: Using complex loops instead of built-in functions.
- Wrong Split: Not using split() or using incorrect delimiter.
- Missing Strip: Not handling leading/trailing spaces.
- Index Errors: Not properly accessing the last element.
- Over-engineering: Using unnecessary data structures or complex logic.
Note: This is a simple string manipulation problem that demonstrates efficient use of built-in string functions.