Hi, everyone.
So in this video, we are going to solve this question, reverse words.
He lost words in a string, so consider this a string, so basically be a string and we have to reverse
the words so the output will be blue, then is then sky then.
So blue is sky.
The OK, so this is the output now consider coding is fun.
Now let us reverse the words so it will be our output will be fun is coding.
So I hope you understood the question.
So this question is basically present.
Only told.
OK, reversing words in a string.
So what we have to do, so the input string, we have to reverse the interesting word by word, so for
the input, the sky is blue, output will be Bluey's Kaida.
So we are reversing the string word by word.
OK, let us take somewhat examples.
So consider these two examples.
OK, so Helliwell, so I have space then haloed in space then.
OK, so after reversing this will be world.
Then a space, then hello, and then basically space.
OK, so you can see so in the reverse string.
Leading and telling the leading and telling spitters should not be dead.
OK, so this is the trailing space, so we will eliminate this space, OK, without this WorldSpace.
Hello.
So this space thing is basically secondary.
OK, so this space thing is basically secondary.
You don't need to worry about this big thing.
We can handle it later on.
OK, first is how to build the logic.
Let us focus on these examples.
The will be example.
Good.
A OK, so this will be the output again, so here there are multiple spaces, there were two spaces,
but in the output there will be only one space.
So we have to reduce the multiple spaces between two worlds into a single space.
So do not worry about spaces for now.
First, let us try to build the logic.
OK, so this space thing is basically secondary.
Now, the logic is very, very simple, this is just the implementation problem.
So what we will do so we'll solve this question in two steps.
So the first step is basically what we will do.
We will reverse each word.
OK, we will reverse each word, and the second step for solving this question is basically reverse
the complete string universe, complete string.
So let us consider this as an example.
OK, so what will reversing each word so after reversing the each word so reverse so it will become
reverse.
So this will become like this.
So this will be a and this will be you Elby be OK.
First step is done.
This was the first step of reversing each word.
Now the second step is basically reverse the complete string.
OK, I will reverse the complete string.
So this will be B, you OK?
Reverse the computing's will be.
So this is B and you then I.
S then why I ask why then the E and you can see we will match the output.
So very, very simple.
We have to follow these two steps.
Now let us for this string, let us perform the first step, reverse each word.
So this will be G.
And I d o c then I have s i then nd you f so the first step is then reversed.
Each word second is reversed, a complete string.
OK, reverse the complete string.
So this will be f you and then I.
S then C or D.
I kn g.
OK, you can see.
Very, very simple.
First, we have to reverse the each word and then we will reverse the competing and how to reverse.
So basically we know how to reverse.
So I will take a start point that I will take and pointer I will start I will say at the start and end,
I will do start placeless.
I will do end minus minus.
OK, so we have done this reverse thing.
OK, so this is very, very simple.
What we do, we will trade over the string.
We will perform the step.
Number one, after performing the step number one, we will get the string and after the string we will
reverse the complete string, OK.
Now after getting this string, after getting the string.
But we have to do we have to handle the space conditions.
OK, so this space condition is basically secondary.
After getting this string, we will modify the string so that the space conditions are met that specified
and negotiated, for example, they should not be leading and trading spaces.
So we will remove leading and trading spaces.
But that is second problem.
OK, so first, let us implement these two.
Reversed each word and then reversed the computer string so we know how to reverse the computer thing,
that is very, very simple.
Now let us see how we can reverse each word.
OK, so consider this a string.
The sky is blue.
The space sky space is blue.
OK, so we know how to reverse the computer thing.
Basically, we know how to do second step.
We have a really solid reversing this string.
OK, now let's see how we can perform the first step, which is basically reversing the words.
OK, so what we will do, I will take two pointer begin pointer and the pointer.
Initially this is my pointer and this is my end point that I will iterate over this string.
OK, so end point related.
So I will come here and they will come here then and they will come here space.
So as soon as the end pointer.
OK, as soon as the end point rent control space.
What we will do, I will reverse the word.
I will call the function reverse.
OK, I will give the string.
Let's do this thing is e I will give the pointer and the end point will be and minus one.
OK, so this will be end and this but president here.
But I have two plus and minus one.
OK, so this word will be reversed then.
What will do.
I will update begin.
So begin will be and plus one.
OK so begin will come here.
OK.
Or you can watch what you can do.
So first what you will do.
You will do and placeless so and will come here and then you will do begin equals end.
OK so begin end the out here again.
I will do the same step and will come here and will come here then and will encounter space.
So as soon as the end encounter space, as soon as the space I will call the reverse function, I will
pass the string, I will pass the pointer.
So this is the pointer I will pass and minus one.
So this is and I will pass and the minus one then the sky will be reversed.
OK, this guy will be reversed.
Then again I will do L'Espresso and will come here.
Begin equals and to begin will come here then.
Then and will reach here.
I will reverse the string then I will reverse this string.
So finally.
After this cold, what will happen, so this will be.
Should then space, then why is that space, then?
S I then space, then you and me, OK, very, very simple.
So what we have to do so as soon as we encounter a space.
What is the meaning of word for word after that word, we will encounter a space.
OK, so as soon as we encounter a space, as soon as we encounter a space, we will reverse the word.
Very, very simple.
OK, so we will reverse all the words.
So these are spaces we will reverse all over.
The special condition is basically this one.
OK, what we are doing, as soon as we are encountering space, then we are reversing, reversing the
word mode for the last word.
Space is not there.
OK, space is not there.
So we will explicitly reverse the last word.
OK, last word is basically the special condition, right?
Especially because I am saying as soon as we encounter a space, we will reverse the word OK.
As soon as I will encounter the space, I will reverse this word.
As soon as I will encounter the space, I will reverse this mode for the last word.
Space is not there.
OK, so if the space is not there, that's why this is a special case.
I have to reverse the last word myself.
OK.
So after the first episode, reversing each word, the second step is basically reversing the complete
string.
OK, I will reverse the complete string.
OK, I will reverse the building, which we have already done, and the third step, which is basically
handling spaces.
OK, so this handling space is very, very simple.
So now let us write the code for first and second.
So I have to return this updated string, so this is string as now first let us write the reverse function.
So the return type of the reverse function will be void.
It will not reverse anything.
It will not return anything.
It will simply modify the given string.
So very important here.
We have to take the string Bio-Reference.
We have to take string Bio-Reference only it is advised by defence and simple as best by default.
OK, only that is by definition C++ by default.
Otherwise you have to write this operator.
OK, I have to pass the string Bio-Reference because I'm going to do the changes in string only, ok.
I will take the beginning and the end point at.
Then I have a very standard code, so we have seen this code many times while being on this list in
order to end what we have to do.
I have to do the slapping part.
So String, I will begin and end.
OK, so I'm stepping begin and end very soon, it could then begin placeless.
And minus minus, so this is very standard.
OK.
We have seen this too many times.
Now, all we have to do, let us perform step one, which is reversing the words.
OK, so step one, reversing the words.
So as we discussed, we will take two pointers.
OK, you can see yourself.
We are taking two pointers begin and end point there.
OK, so initially begin with the president at zero index similarly and will also be present at index
zero.
Simple, then I told you we will address the end point.
OK, you can see yourself the end point that is moving ahead to the speech.
OK.
So and and is basically let's change the name, let's name it a.
So while and is basically less than eroticize.
OK, so what we have to do.
So if.
The end blender is basically encountering a space, so as discussed, what we have to do, we have to
call the reverse function, OK, you can see yourself whenever the end pointer is basically space.
I have to call the reverse function, I have to pass string pointer and a minus one, OK, and is basically
space, so I will pass and a minus one.
So this is there end of minus one.
OK, this is begin.
This is an A minus one.
I will end placeless.
So that and will come here and then I will begin equals end, OK?
Simple.
I'm calling the reverse function, I am putting the string, then I have to pass the beacon, I have
to pass and minus one.
OK, I have to pass and my nisson then I will do and placeless and then I will begin equals goaltend.
Simple now in the LS part, the current characters in Artspace, then I will do placeless and will simply
move ahead.
OK, now, very important condition.
OK, I have to reverse the last word.
I have to reverse the last word of myself.
So let us reverse the last word.
So what I will do, I will call the reverse function.
I will pass a string.
Then I have begun pointer and and minus one.
So this is basically for the last word.
This is for the last word, last word is basically special then after doing this.
I will step one is done.
OK, so basically step one is then now step one.
But I have to do I have to reverse the computer.
OK, you can see step one is basically reversing each word.
So they were saying each word is done now, reverse the computer string, reverse the computer string,
so I will call the reverse function, I will pass the string start, will we begin?
Will be zero and and will be so and will be eroticize minus one and minus one.
Adult size minus one.
So basically second step is also then.
OK, reversing the complete string.
Reversing complete string.
Now, what we have to do, we have to handle spaces, OK, if you in the cold, let's first round the
clock.
OK, sorry, I'm not pretending anything I have to return, OK, I have to return, so I'm not returning
anything.
So first.
OK, so let's get it done.
I am doing changes in only OK.
I'll give this one, it is working because this is not space's now let us submit cold.
OK, so this will give us added because we do not handle the species condition.
OK.
So you can see yourself there are leading spitters.
So I have leading spaces in my output.
OK, so I have to remove the leading and the building spaces and also if there are multiple spaces in
between, if there are multiple spaces in between, I have to convert the multiple spaces into a single
space.
OK, now let us write the code for handling spaces.
So this is pretty much a straight forward, so let us consider this, make us bring help, so.
First, what we have to do, we have to convert multiple spaces into a single space, so let us write
code for converting multiple spaces and do single space.
OK, so what I'm planning to do here is so if we have multiple spaces, I have to consider only one
space and I have to discard all of those spaces.
OK, so this is the first condition.
Second condition is basically I have to remove leading and I have to remove trailing spaces.
I will handle it later.
First, let us handle this, OK?
I have to convert multiple spaces into a single space.
So let's see if this index is basically so I will store one space and I will move ahead.
OK, so I will do a shapelessness till spaces are there.
OK, simple.
Now let us write the code.
So first I'm converting multiple space into a single space.
So while I is basically less than Aeronaut says.
OK, so what we have to do so is basically the current character is a space if the current character
is basically a space, what we have to do, I have to consider I have to consider one space.
I can consider one space.
So help dot.
Pushback when space.
OK, I will move ahead, I placeless now if.
While Alphie, so if there are multiple spaces, I have to avoid them.
OK, so if there are multiple spaces while F.I. equals equal space, what I have to do, I have to avoid
them.
So I will do a plus plus simple.
Now, in the end part.
But we have to do so in the early part, this is not a space character.
This is a normal character.
So if this is a normal character, I can take this character.
So I brought push back if I and I will move ahead, I placeless.
OK, so basically.
What I'm doing here, so let's make it help.
OK, so this is help.
So what I'm doing here is basically I'm converting multiple spaces into a single space.
OK, I'm converting multiple spaces into a single space.
Now I have to do something for drilling and living spaces.
OK, so if.
I encountered a space I am digging that space character, so for example, this is let's call your Dimi
Space Space fun.
So I will consider your name as soon as I encounter space, I am taking that character, I am moving
ahead now.
Suppose if there are multiple spitters, so I have to avoid all these spaces and then I have to reach
Eugh.
OK, so this is the code.
This is the line for avoiding multiple spaces.
OK, so after this line, let us consider this example, so sky, then space, space blue and then I
have multiple spaces.
OK, so suppose this is the output now after this cold.
So this output will be changed to single species sky and the go blue and then a single space.
OK, so these three spaces.
So these were basically the trailing.
Spaces.
OK, so if there are multiple trading spaces now, there is only one single trading spaces.
OK, so if there are multiple leading spaces, so these are leading spaces.
So if there are many leading spaces now, there are only one living space.
OK, simple.
So this is the string.
OK, now let's do something with this.
OK, so first thing before converting this multiple spaces into a single space, let us get rid of this
living spaces.
OK, so how to get rid of this living spaces?
Very, very simple.
So leading spaces will present at different.
So while Alpha equals equal space.
So this is four leading spaces.
I will do a placeless.
OK.
So this is for the leading species.
Now, leading species has been handled.
Multiple services has been converted into single space.
Now we will handle trading spaces, the spaces which will be present at the last now after discord,
if there are multiple trading spaces.
I have only one single dwelling space.
OK, that will present at the last.
So help dart back.
So I have this function back.
It will give me the last element that will give me the last character.
So over the last couple of days, basically space.
If the last quote is basically space, that means I have to pop this character, I do not need trading
spaces, so help Bob back until just like push back function.
I have Bob back function, so I will remove the last character.
OK, so what I am doing here is basically.
So consider this string.
Sky space, space, space, blue, and then space, space, space.
OK, so what this land will do, so it will take care of living spaces.
So this is leading, OK, spelling mistake.
So what will happen?
I will reach here, OK?
This will be my eye.
All these two spaces will be removed.
OK, then, after this cold.
This will convert multiple spaces into a single space, so it will look like this when space then blew,
then all this three spaces will be converted into a single space.
OK, so this is the output after this called.
Now, what I'm doing back, so help this is basically help sting, OK, this is help sting, so help
the dog back.
So this is the last element.
So back element will give me the last element.
OK, so this Beckers and function, it will give me the last element or last element is basically space.
So if the last character is space, what I'm doing helps.
Bombeck So aFunction push back and aFunction Bombeck.
OK, so what will happen.
So this last element will be popped.
OK, this will be removed.
Now my string is basically sky and the called blue.
And I'm returning that string simple.
So I'm retaining that strength.
OK, so I think our culture and fine that does Amitabha good.
OK, so our goal is working fine, so this is basically leading leading spitters and this is trading
spaces.
So this court is for converting multiple spaces into a single space.
Multiple space and two single space.
OK, now let's discuss the time and the space complexity of our solution, so what we are doing, we
are just using the reverse function.
We are trading over the string and we are using the reverse function.
So what is the time, complexity, time complex?
It is simply big often.
OK, so time complexity is big often.
And this is complexity is basically big of one.
OK.
So we have to reverse.
We have to retain our strength, so I created this strong help and I'm reversing.
I am returning that string.
OK, so space complexity, we can say it is big of one.
OK, so this is finally the time and the space complexity.
So if you have any doubt in this question, you can ask me.
OK, thank you.