1
00:00:00,590 --> 00:00:04,370
So we have successfully coded the first method that we discussed.

2
00:00:04,400 --> 00:00:07,580
Now let's take a sample string input.

3
00:00:07,580 --> 00:00:12,260
Let's say it's d e f d e f and see what's happening in this code.

4
00:00:12,260 --> 00:00:12,650
All right.

5
00:00:12,650 --> 00:00:17,000
So initially we create a empty string new string to compare.

6
00:00:17,000 --> 00:00:18,620
So that's this one over here.

7
00:00:18,620 --> 00:00:19,850
It's empty initially.

8
00:00:19,850 --> 00:00:21,980
Now we enter into the for loop.

9
00:00:21,980 --> 00:00:25,040
Now I is set to string dot length minus one.

10
00:00:25,040 --> 00:00:26,720
Over here the length is five.

11
00:00:26,720 --> 00:00:32,510
So let's just quickly write the indices zero, one, two, three, four and five.

12
00:00:32,510 --> 00:00:33,560
So the length is six.

13
00:00:33,560 --> 00:00:36,140
In this case so six minus one is five.

14
00:00:36,140 --> 00:00:39,800
So initially the pointer is set to this one over here right.

15
00:00:39,800 --> 00:00:45,770
So we take the character over here and we append it to the empty string which we created over here.

16
00:00:45,770 --> 00:00:45,950
Right.

17
00:00:45,950 --> 00:00:48,890
So f is put appended into the empty string.

18
00:00:48,890 --> 00:00:52,460
And then we move the pointer to the left.

19
00:00:52,460 --> 00:00:52,760
Right.

20
00:00:52,760 --> 00:00:54,770
We have I minus minus over here.

21
00:00:54,770 --> 00:00:56,360
So we move it to the left.

22
00:00:56,360 --> 00:01:00,620
We take the character at index four which is e, and append it over here.

23
00:01:00,620 --> 00:01:04,400
And we keep doing this over and over again for the complete string.

24
00:01:04,400 --> 00:01:08,570
So we get the new string to compare f e d f e d.

25
00:01:08,600 --> 00:01:09,140
Right.

26
00:01:10,260 --> 00:01:16,020
Now, at this point, we will check whether this and this over here is the same or not.

27
00:01:16,020 --> 00:01:19,230
And we see that it's not the same, so it's not the same.

28
00:01:19,230 --> 00:01:20,370
So we will return false.

29
00:01:20,370 --> 00:01:21,810
So that's what's happening over here.

30
00:01:21,810 --> 00:01:23,730
If it was the same we would have returned.

31
00:01:23,730 --> 00:01:24,090
True.

32
00:01:24,090 --> 00:01:27,600
So that's how we solved this using the first method.

33
00:01:27,600 --> 00:01:34,260
And notice the important thing that we took away from here is we saw in a previous video that when it

34
00:01:34,260 --> 00:01:39,450
comes to the string data structure in languages like JavaScript, these are immutable.

35
00:01:39,450 --> 00:01:44,970
So whenever we do this append operation it's an O of n operation right time.

36
00:01:44,970 --> 00:01:51,780
It's an O of n time operation because we're actually traversing the string, copying it and adding the

37
00:01:51,780 --> 00:01:53,700
new character and creating a new string.

38
00:01:53,700 --> 00:01:54,060
Right.

39
00:01:54,060 --> 00:01:59,130
So it's not like adding an element to an array, which is a constant time operation over here.

40
00:01:59,130 --> 00:02:02,820
Appending a character to a string is an O of n time operation.

41
00:02:02,820 --> 00:02:06,570
And we are doing this again and again for each element.

42
00:02:06,570 --> 00:02:09,780
So we have n elements in the input string.

43
00:02:09,780 --> 00:02:14,970
So that's why the time complexity is n into n which is o of n square.

44
00:02:14,970 --> 00:02:15,360
Right.

45
00:02:15,360 --> 00:02:18,120
So this is not a good time complexity.

46
00:02:18,120 --> 00:02:20,490
And what about space complexity.

47
00:02:20,490 --> 00:02:24,060
We are creating a new string over here which is of length n.

48
00:02:24,060 --> 00:02:27,810
If the input string is of length n, the new string is also of length n.

49
00:02:27,810 --> 00:02:31,110
So the space complexity of this solution is o of n.
