Length Of Last Word Leetcode Problem 58 [Python Solution]
Welcome to another coding adventure!
Today, we'll tackle a relatively easy problem: Length Of Last Word If you're a beginner, this is a great challenge to enhance your coding skills.
Try solving it on your own, and then compare your solution with the one we'll explore together.
Problem Overview
The problem is simple yet essential.
You're given a string s
consisting of words and spaces.
Your task is to return the length of the last word in the string.
A word is defined as a maximal substring consisting of non-space characters only.
Example 1:
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with a length of 5.
Example 2:
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with a length of 4.
Example 3:
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with a length of 6.
Understanding the Constraints
Before diving into the solution, let's understand the constraints given in the problem statement:
1 <= s.length <= 104
: The length of the input strings
will be between 1 and 104 characters.s
consists of only English letters and spaces ' '.- There will be at least one word in
s
.
Now that we have a clear picture of the problem, let's explore a Python solution.
Length of Last Word Python Solution
Here's a Python solution to find the length of the last word in the given string:
def lengthOfLastWord(self, s: str) -> int:
# Initialize pointers
i = len(s) - 1
length = 0
# Eliminate leading white spaces
while i >= 0 and s[i] == ' ':
i -= 1
# Count the characters of the last word
while i >= 0 and s[i] != ' ':
length += 1
i -= 1
return length
This Python function uses two pointers, i
and length
, to efficiently solve the problem.
1. Eliminating Leading White Spaces
We start by setting i
to the end of the string (the last character) and length
to 0. We then enter a loop to eliminate any leading white spaces.
As long as i
is within bounds (greater than or equal to 0) and the character at index i
is a space, we decrement i
.
This loop ensures that we skip any leading spaces in the string.
2. Counting the Characters of the Last Word
Next, we enter a loop to count the characters of the last word.
While i
is within bounds and the character at index i
is not a space (i.e., part of the word), we increment the length
by 1 and continue moving backwards by decrementing i
.
This loop counts the characters of the last word.
Once we exit the loop, we have the length of the last word, and we return this length as the result.
Time and Space Complexity
Time Complexity
The time complexity of this solution is O(n)
, where n is the length of the input string s
.
We iterate through the string once to eliminate leading spaces and count the characters of the last word.
In the worst case, we may have to traverse the entire string.
Space Complexity
The space complexity is O(1)
because we use only two extra variables, i
and length
, to keep track of our progress.
These variables use constant memory.
Reasoning Behind Our Approach
Our approach is straightforward and efficient.
We start from the end of the string and work our way backward, eliminating leading spaces and counting the characters of the last word.
This approach avoids unnecessary iterations through the entire string, making it both time and memory-efficient.
Related Interview Questions By Company:
Related Interview Questions By Difficulty:
Related Interview Questions By Category:
- Remove Duplicates From Sorted Array II LeetCode
- Range Sum Query 2D Immutable LeetCode
- Longest Turbulent Array LeetCode
Conclusion
In this guide, we've explored an easy yet valuable coding problem: Length Of Last Word We provided a Python solution that efficiently finds the length of the last word in a given string.
Understanding and addressing the problem constraints is a crucial step in problem-solving.
We used two pointers to eliminate leading spaces and count the characters of the last word, resulting in a clean and efficient solution.
If you're a beginner, practicing such problems will help sharpen your coding skills.
Don't hesitate to explore different approaches and share your solutions and suggestions.
Remember that coding is a continuous learning journey.
If you found this guide helpful, please like and engage to support our our platform.
Feel free to comment, ask questions, and share your thoughts.
We encourage a thriving coding community, where everyone can learn and grow together.
For the full problem statement and more details, you can visit the Length of Last Word LeetCode problem.
Happy coding!