Leetcode 58 - Length of Last Word
02-Jul-2026
I recently got a fitbit air and it’s been pretty itneresting to see my health readings so clearly quanitified. It even says my readiness is the best today?! Pretty interesting ngl. I still don’t have a hang on the tracking my calories, but it shouldn’t change much from what I know.
Python had useful functions C++ doesn’t have
This question asks to return the last word in a string with the possibilitiy of different number of whitespace. In python, this would be solved real quick by trimming the string or to make an array of all the words in the string and setting the whitespace as the trimming points. With this, we can just simply return the last element. I found that C++ doesn’t have this..
With that I did some digging. There are some functinos that C++ does have that I could use instead.
If I wanted to return an array of words in a sequence, I could take int input string as a string stream
and add all the words separately to a vector<> container. And by using the operator>> for this
stream, it will trim all the whitespace on its own, returning a vector of just words.
The other method would be to strictly return the last word, saving (where is the number of
words in the string) space. There are two functions we use in this case, the for_if_not() and for_if.
With these, we can check from the last character of the string, which position holds the very first
non-whitespace character, and the start of the first charcter in the last word. This way we get the
last word returned right away.
Performance evaluation
The time complexity of these two solutions is where is the number of characters/words. As for space complexity, the first introduced method takes additional space while the second method takes only .