Hi everyone, we have successfully coded the third method.
Now let's take a sample input and quickly walk through what's happening and see what's happening in
the code.
So let's say the input string is d e, f, e, d and the indices of the string.
Let's quickly write that it's 01234.
Now what we are doing is initially I is zero and j is equal to string dot length minus one.
So the length of this string is five.
So five minus one is equal to four right.
So I is pointing to index zero and j is pointing to index four.
Now while I is less than equal to j we keep repeating what's there inside the while loop right.
So we check whether these two characters are the same.
And over here we have D.
And over here also we have D because they are the same we move forward.
If not, we would have returned false.
Now because they are the same.
What we are doing is we are incrementing I and Decrementing J.
So I becomes one and j becomes four.
And again we check whether the characters over here are the same again because they are the same.
We increment I and decrement j.
Now both are pointing to index two.
Therefore definitely they will be the same.
So we won't come into this part.
We'll come again to else.
Now at this point J will become one and I will become three.
Right now at this point we break because I is no longer less than or equal to J.
Or you can see that I is greater than J.
So we break and we come over here and we return true.
Right.
So that's how we solve this question.
And we have seen that the time complexity over here is O of n and the space complexity is O of one.
The time complexity is O of n because we are just traversing the given string once, right?
In fact, we could say that it's o of n by two, but then this is just a constant.
So we remove it.
So the time complexity is O of n and we are not using any additional space.
So the space complexity of this solution is O of one.