Introduction
I am still on the journey of solving at least one Data Structures and Algorithm question daily on Leetcode in order to understand the concepts I am learning well and it has been very challenging and interesting since I started solving these questions.
This article will explain my solution to Length of Last Word in a String on Leetcode and the logic behind my solution. Try to follow it step by step and I am sure you will understand every bit of it.
Let's dive into it ๐
Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
Example
- Input: s = "Hello World"
- Output: 5
- Explanation: The last word is "World" with length 5.
Step by Step Solution
Solving a Data Structures and Algorithm question requires you to think well and check your solution using different scenarios because your solution must pass all the test cases before it can be accepted.
We will solve this problem in 2 steps, which are:
- Doing Sanity Checks.
- Explaining the Logic behind our solution and writing the Code to back it up.
Sanity Checks
Before proceeding to carry out any operation we have to check the input passed to the function if it is valid. so let's do a minor check that if it is not valid.
// Sanity Checks
if (!s) {
return s;
}
The last check is if the length of the string is 1, just return the length of the string directly because it is also the last word.
if(s.length <= 1) {
return s.length;
}
But if none of these conditions is met then we can proceed to carry out the main operation.
Solution Logic
The Solution to this question will be in steps for easy understanding.
Trim the string with the trim method
trim()
in order to remove whitespaces from the left and right sides of the string.After removing the whitespaces with trim then we can now turn the string to an array so that it will be very easy to get the last value of the array by index.
To turn the string to array, we will use the split method
split(" ")
. There must be space between the double quote so that it can split it by words, if there is no space between the quote it will split it by character.
s.trim().split(" ");
- Let's store this new array in a variable called
arrStr
let arrStr = s.trim().split(" ");
The next thing is to get the last item in the array of words, we can get it by the index and to get the index of the last item in every array the index is always the length of the array - 1 because array starts the index from 0.
So since the index is
[arrStr.length - 1]
. then the last item of the array can now be gotten like this
arrStr[arrStr.length - 1]
- Since we have gotten the last item, we can just use the
.length
method to get the length.
arrStr[arrStr.length - 1].length;
- Lastly, return the length of the last word.
return arrStr[arrStr.length - 1].length;
The Time and Space Complexity
Time Complexity
This Solution has a Time complexity of O(n) which is a Linear time since we are splitting the string to an array and it depends on the length of the string.
Space complexity
The Space Complexity is also O(n) since we are using extra space to store the words in an array.
The Runtime is 60ms, faster than 98.80% of JavaScript online submissions for Length of Last Word and Memory Usage: 38.8 MB, less than 47.63% of JavaScript online submissions.
Conclusion
I hope you have learnt a lot from this article and your contribution is also highly welcomed because we get better by learning from others, you can reach out to me on Twitter here. Watch out for more helpful contents on Data Structures and Algorithm from me.
Don't forget to Like, share and subscribe for more articles in this series called Solutions to Data Structures and Algorithm Questions on Leetcode.
Enjoy ๐