Hi, everyone.
So today we are going to solve this question, find the length of the last word.
OK, so the input will basically be a string.
Input will basically be a string and we have to find the length of the last word.
OK, so let us consider some example.
So if I were in Putin's.
Hello.
So these are is OK.
So if I were in Putin's hello space based world, then this is the last word.
OK, so this is the last road and the land of this word is basically five.
OK, so our output for this example will basically be a five the length of the last word, which is.
Well, and it's Lantis five.
OK, now this is an empty string so far and protesting, obviously the last word does not exist.
So our output will be zero of the last word is basically zero.
Now let us consider this input.
So for this input, we have a space here.
Then we have a word, then we have a word world.
Then we have spaces again.
OK, so which is the last word?
So this is basically the last word and this is also the first word and this is also the last word.
So basically the length to the last word is five.
So our output for this input is basically a five.
OK, so this is simply an implementation problem.
OK, this is simply an implementation problem.
So what we will do so we will start from the beginning of the string.
So suppose this is a very large string.
So what we have to do so we will start from the beginning of the string.
We will like trade, we will maintain a variable count, which we will update.
OK, so as soon as we encounter a space, as soon as we encounter space, so what we will do, we will
make this count variable zero.
OK.
And then when we will encounter the variable again, we will encounter another character.
We will increment we will update this current variable.
OK, so this is simply an implementation problem.
Now let us write the code, OK?
We can write the code.
Basically, this is a little problem.
OK, so you can read the problem statement.
So the input will basically be a string that can be empty spaces and we have to return the length of
the last word.
OK, so now let us write the code.
So first, let us create a variable count which will store our answer.
OK, the second variable will store the length of the last word.
So what I'm doing here is so basically there are many ways to implement the solution for this problem.
So what are the ways?
So first, what can do?
You can reverse the string.
So if you will reverse the string.
So the problem is very simple.
You have to find the length of the first word.
OK, you have to find the length of the first word, if you will, reverse the string.
Second approach is basically start iterating from the end of the string, if you will, start iterating
from the end of the string.
What do you have to do?
You have to find the length of the first word from the end, OK?
You have to find the length of the first word from the end, if you will start updating from the end.
OK.
You have to find the Lenthall the first word from the end, if you will start outrating from the end
of the string and the second the third way to implement this problem is basically start outrating from
the first, OK, start dating from the first.
So there are many ways to implement this problem.
So let's say I am implementing this approach.
OK, so these two approaches are very simple to implement.
So this is a little bit difficult, I think.
So let me implement this approach.
OK.
So I have a variable count, which is initially zero.
Now let us find out the size of the string.
So as nazis'.
OK, so let us take a appointer.
So we are dating from the start of the spring, so equal zero.
So while I is listening.
OK, so first of all, check whether this character is a space character or any other character.
So if this character is not space.
So if this is not space, I can safely do countless plays, OK, because this character is not a space,
so I can do countless plays, I can do ableist.
Let's move ahead.
OK.
Now in the Elzbieta.
So the current character is basically be so.
The current character is Aspies.
OK, so while the current characters are space, so.
While the current character.
Is space, what can I do, I can do a plus plus.
OK, so what I'm doing here is.
So what I'm doing here is, for example, you have a string hello?
Then you have space and then you have multiple spaces and then you have the word blue.
OK, so if you will encounter a character.
So at this point, if I if I am going to space, so while that while the character space I am moving,
I had to move ahead.
Move ahead, move ahead, move ahead.
Move ahead and hit.
I will stop.
OK, because this is not space OK.
But while writing this code you have to also write the condition I is less than an otherwise you may
get around.
OK.
We have to right this condition, otherwise you can't get segmentation fault, although you can get
runtime added.
So now let us ride that condition also.
So while I is less than in and basically the characters space, what they can do, you can do a plus
plus.
Now, there are two ways to break this wide loop.
First, they become sequels to end.
OK, so if.
I become so close to end, that means we reach the end of the string.
OK, so we have reached the end of this thing, evidence on this besant in the variable count so you
can return count.
OK, so we have reached the end of the string.
OK, so and our answer was the variable count, so I would have done count now in the L'ESPRIT.
I have not reached the end of the string and there are still characters.
So what I will do, I will make count equals zero.
OK, so the meaning of this is basically you have you didn't reach the end of the string and there are
still characters.
So basically we have to make the current variable zero because there are still characters present.
OK, and finally.
What we will do so when this valuable land, I can return my answer, which is present in the current
variable.
OK, so now let us first.
Then I will call and then we will try to submit our code, OK?
Now only does some good.
OK, so Al Gore is looking fine now let us discuss the time and the space complexity for our solution.
So as we can see clearly, we are just creating only a few variables.
So the space complexity is basically constant.
So this is our one big off one and the time complexities linear because we are just getting over the
string.
OK, so we are simply iterating over the string.
So the time complexity is big off and and the space complexities are the rough one.
OK, so if you have any doubt in this question, you can ask me.
OK, thank you.