Hi everyone.
Let's now code together the third method that we discussed.
Now for this we will write a function.
Let's call it is palindrome check.
And this function takes in a string.
Now what we are going to do is we are going to initialize two pointers.
Now let the first pointer be I.
And that's set to zero which is the beginning of the input string, the index of the beginning right.
And the second index let's call it j and we will initialize it to two string dot length minus one.
So this is the index of the last character of the input string.
Now we are going to run a loop.
And this will go on till I less than equal to J.
Right.
So we have seen the two cases in the discussion.
If the length of the input string is an even number.
So let's just quickly see that over here.
Let's say the input string is a b a b b a right.
So this is of length four.
And the other case is let's say it's a b c b a.
Now over here initially I will be a and j will be a, then I will be B and j will be B, and then afterwards
when we do I plus plus and j minus minus, you can see that I will become greater than j.
So we need to stop at this point.
So that's why we have I less than J.
And in the case of an a string with length odd number like for over here the length of the string is
five.
So in this case initially I will be at this pointer like at zero right.
And J will be over here pointing to this character.
Then I will point to this character and J will point to this character, and then both will point to
the middle character or it got deleted.
Okay.
So both will point to the middle character C and afterwards we need to stop right?
When they change the uh, when it becomes I will become greater than J.
So we could also write, while not I greater than J, it's both the same.
While I less than equal to j is the same as while I not greater than j, so it's the same.
So this will keep repeating while this is true.
All right.
Now when this is true, what are we going to do.
We are going to compare the character at the ith and jth index.
So string I if this is not equal to string of j.
So if these are not equal, then it's definitely not a palindrome.
Right.
So we can just return false.
So we come out of the function.
Now if this is true if these are true right.
For example let's say we have some characters in between.
And the first one is a the character of the first character is a, and let's say the last character
is a.
So at this point we see that both of them are equal.
So what do we do.
We need to increment I and we need to decrement j.
Right.
So let's do that.
So if it's not true that that is if these two characters are true then what do we do.
We have an else part over here in this part what we do is we increment I so that this pointer, whatever
was pointing over here goes to the right.
And we decrement j right.
So that whatever was pointing over here goes to the left.
So j minus minus.
And then that's it.
So if we come out of this loop without returning false that means that it's true.
Right.
So we return true over here and we have our function.
Now let's test it.
Let's call our function and check whether it's working.
So to this function let me pass the string ABC.
So this is ABC.
Let's now see what we get when we run this.
When we run this we'll get false because this is not a palindrome.
Right now if we have BA over here we get true because this is a palindrome.
Again, if I have AA, I get true.
If I have a A, I get false because this is not a palindrome.
So yes, this solution is working.
And you can see that this is a solution which runs in a time complexity of O of N, right.
So we can see that we just have this loop over here and we are traversing the given string only once.
Right.
In fact it's o of n by two.
Right.
Because we're going from both the sides.
But then one by two is a constant.
So we leave that right.
So the time we can just ignore that.
So the time complexity of this solution is O of n and the space complexity is O of one.
Right.
We are not creating a new array or a new string like we saw in the previous solutions.
So the space complexity of the solution is O of one.