So we have successfully coded the first method that we discussed.
Now let's take a sample string input.
Let's say it's d e f d e f and see what's happening in this code.
All right.
So initially we create a empty string new string to compare.
So that's this one over here.
It's empty initially.
Now we enter into the for loop.
Now I is set to string dot length minus one.
Over here the length is five.
So let's just quickly write the indices zero, one, two, three, four and five.
So the length is six.
In this case so six minus one is five.
So initially the pointer is set to this one over here right.
So we take the character over here and we append it to the empty string which we created over here.
Right.
So f is put appended into the empty string.
And then we move the pointer to the left.
Right.
We have I minus minus over here.
So we move it to the left.
We take the character at index four which is e, and append it over here.
And we keep doing this over and over again for the complete string.
So we get the new string to compare f e d f e d.
Right.
Now, at this point, we will check whether this and this over here is the same or not.
And we see that it's not the same, so it's not the same.
So we will return false.
So that's what's happening over here.
If it was the same we would have returned.
True.
So that's how we solved this using the first method.
And notice the important thing that we took away from here is we saw in a previous video that when it
comes to the string data structure in languages like JavaScript, these are immutable.
So whenever we do this append operation it's an O of n operation right time.
It's an O of n time operation because we're actually traversing the string, copying it and adding the
new character and creating a new string.
Right.
So it's not like adding an element to an array, which is a constant time operation over here.
Appending a character to a string is an O of n time operation.
And we are doing this again and again for each element.
So we have n elements in the input string.
So that's why the time complexity is n into n which is o of n square.
Right.
So this is not a good time complexity.
And what about space complexity.
We are creating a new string over here which is of length n.
If the input string is of length n, the new string is also of length n.
So the space complexity of this solution is o of n.