1
00:00:00,450 --> 00:00:03,720
Hi everyone, we have successfully coded the third method.

2
00:00:03,720 --> 00:00:08,880
Now let's take a sample input and quickly walk through what's happening and see what's happening in

3
00:00:08,880 --> 00:00:09,390
the code.

4
00:00:09,390 --> 00:00:14,580
So let's say the input string is d e, f, e, d and the indices of the string.

5
00:00:14,580 --> 00:00:17,310
Let's quickly write that it's 01234.

6
00:00:17,340 --> 00:00:22,740
Now what we are doing is initially I is zero and j is equal to string dot length minus one.

7
00:00:22,740 --> 00:00:24,510
So the length of this string is five.

8
00:00:24,510 --> 00:00:26,730
So five minus one is equal to four right.

9
00:00:26,730 --> 00:00:30,990
So I is pointing to index zero and j is pointing to index four.

10
00:00:30,990 --> 00:00:37,020
Now while I is less than equal to j we keep repeating what's there inside the while loop right.

11
00:00:37,020 --> 00:00:40,560
So we check whether these two characters are the same.

12
00:00:40,560 --> 00:00:42,120
And over here we have D.

13
00:00:42,120 --> 00:00:45,450
And over here also we have D because they are the same we move forward.

14
00:00:45,450 --> 00:00:47,340
If not, we would have returned false.

15
00:00:47,340 --> 00:00:49,260
Now because they are the same.

16
00:00:49,260 --> 00:00:52,380
What we are doing is we are incrementing I and Decrementing J.

17
00:00:52,380 --> 00:00:55,050
So I becomes one and j becomes four.

18
00:00:55,050 --> 00:00:59,910
And again we check whether the characters over here are the same again because they are the same.

19
00:00:59,910 --> 00:01:02,070
We increment I and decrement j.

20
00:01:02,070 --> 00:01:04,830
Now both are pointing to index two.

21
00:01:04,860 --> 00:01:06,870
Therefore definitely they will be the same.

22
00:01:06,870 --> 00:01:08,670
So we won't come into this part.

23
00:01:08,670 --> 00:01:10,080
We'll come again to else.

24
00:01:10,080 --> 00:01:14,550
Now at this point J will become one and I will become three.

25
00:01:14,550 --> 00:01:19,470
Right now at this point we break because I is no longer less than or equal to J.

26
00:01:19,470 --> 00:01:21,450
Or you can see that I is greater than J.

27
00:01:21,450 --> 00:01:25,260
So we break and we come over here and we return true.

28
00:01:25,260 --> 00:01:25,620
Right.

29
00:01:25,620 --> 00:01:28,320
So that's how we solve this question.

30
00:01:28,320 --> 00:01:34,080
And we have seen that the time complexity over here is O of n and the space complexity is O of one.

31
00:01:34,080 --> 00:01:38,910
The time complexity is O of n because we are just traversing the given string once, right?

32
00:01:38,910 --> 00:01:43,230
In fact, we could say that it's o of n by two, but then this is just a constant.

33
00:01:43,230 --> 00:01:44,100
So we remove it.

34
00:01:44,100 --> 00:01:48,570
So the time complexity is O of n and we are not using any additional space.

35
00:01:48,570 --> 00:01:51,510
So the space complexity of this solution is O of one.
