1
00:00:01,630 --> 00:00:08,470
Before we cut out our optimal solution, I want to remind you that we have the final code inside of

2
00:00:08,470 --> 00:00:11,010
our resources folder in some Reppel file.

3
00:00:11,290 --> 00:00:15,670
If you check our resources folder and open it up, you can always use that as a reference while we're

4
00:00:15,670 --> 00:00:16,440
writing this out.

5
00:00:17,480 --> 00:00:22,850
Diving into it here, I've just initialized the function and I've placed the two strings we want as

6
00:00:22,850 --> 00:00:26,990
reference, the first step to our optimal solution is initializing.

7
00:00:26,990 --> 00:00:32,960
These two points are values that point to the last index of the character in the string.

8
00:00:33,770 --> 00:00:43,220
So here we can say, let's p one equal estcourt length minus one, because length minus one is the index

9
00:00:43,220 --> 00:00:44,360
of that last character.

10
00:00:44,990 --> 00:00:48,830
We're going to do the same thing for T and I'm going to call this P to.

11
00:00:49,900 --> 00:00:53,950
And it's going to be equal to tea length minus one.

12
00:00:55,500 --> 00:01:02,370
Now, what we want to do is we want to write some kind of conditional loop that will do the evaluation

13
00:01:02,520 --> 00:01:05,220
and decide on what to do with P1 or P2.

14
00:01:05,610 --> 00:01:11,130
The difference here is that we kind of need code to govern each one's responsibility, even though it's

15
00:01:11,130 --> 00:01:13,820
similar and you'll see what we mean once we actually write it.

16
00:01:14,490 --> 00:01:21,540
But one big thing to remember is that we want to check for false cases first, because we might know

17
00:01:21,570 --> 00:01:26,880
with this character whether or not this function needs to return false, because if these characters

18
00:01:26,880 --> 00:01:29,120
don't match, then we know we return false.

19
00:01:29,340 --> 00:01:32,160
But if they do match, we don't know that we can return.

20
00:01:32,160 --> 00:01:32,470
True.

21
00:01:32,490 --> 00:01:34,350
We have to check the rest of the string first.

22
00:01:34,620 --> 00:01:39,210
So that's why we're going to do a condition check for false when we compare these characters.

23
00:01:39,870 --> 00:01:44,310
So what we're going to say is that while P one.

24
00:01:45,930 --> 00:01:48,330
Is greater than or equal to zero.

25
00:01:49,290 --> 00:01:55,140
So in this case, we want to make sure that P one is within the indices of these strengths, if it's

26
00:01:55,140 --> 00:01:59,820
passed, then technically P one is done being calculated for.

27
00:02:00,630 --> 00:02:08,280
But we also want to say or P two is greater than or equal to zero as well, because we want to make

28
00:02:08,280 --> 00:02:11,100
sure that we're accounting for both these cases.

29
00:02:12,020 --> 00:02:18,020
If there's a chance that one of these strings is significantly longer than the first one, even after

30
00:02:18,020 --> 00:02:19,580
both these strings are transformed.

31
00:02:20,480 --> 00:02:27,140
We want to make sure that when PETA advances, let's say PETA is the longer string, if PETA advances

32
00:02:27,140 --> 00:02:33,980
and P one is already at the end, then what's going to happen is that it should be some kind of comparison

33
00:02:33,980 --> 00:02:38,360
that returns false because p one is done, there's no more characters.

34
00:02:38,360 --> 00:02:41,150
And if T still characters, then of course we return false.

35
00:02:41,780 --> 00:02:47,720
So by wrapping this while condition here, we can see how that will happen a little later as well.

36
00:02:47,730 --> 00:02:53,600
But this is just the condition of how we're going to satisfy to make sure that these two strains are

37
00:02:53,600 --> 00:02:55,640
also equal even in their final forms.

38
00:02:56,730 --> 00:03:02,670
So let's just say that here we're doing this while check, and what we want to do now is we want to

39
00:03:02,670 --> 00:03:09,630
write a condition that checks whether or not P one or P two are pointing to hashes.

40
00:03:10,510 --> 00:03:12,070
So here will say if.

41
00:03:13,450 --> 00:03:14,800
S at P one.

42
00:03:16,380 --> 00:03:19,470
Is equal to a hash.

43
00:03:21,380 --> 00:03:23,330
Or tea.

44
00:03:24,370 --> 00:03:28,320
AP two is also equal to a hash.

45
00:03:33,690 --> 00:03:38,910
What we're going to do is just say for this if condition, if any of these characters point to a hash,

46
00:03:38,910 --> 00:03:44,190
we don't want to compare the characters yet because if any of these compared to a hash, we have to

47
00:03:44,190 --> 00:03:45,270
move these pointers.

48
00:03:45,300 --> 00:03:53,340
We don't want to compare unless we know for certain P1 and P2 are pointing at characters that will definitely

49
00:03:53,340 --> 00:03:55,630
be in the final versions of the strings.

50
00:03:56,280 --> 00:04:03,330
So by writing this if check, we then step in and write our code for both P1 and P2 and here we'll say

51
00:04:03,330 --> 00:04:05,340
if s at P1.

52
00:04:07,300 --> 00:04:08,650
Is equal to a hash.

53
00:04:09,820 --> 00:04:13,750
So this is the code to govern this first one.

54
00:04:15,420 --> 00:04:17,850
We will say, let's back count.

55
00:04:20,690 --> 00:04:21,710
Equals to.

56
00:04:23,430 --> 00:04:29,940
Now, background is going to be how we decide how many units or how many indices we need to move P one

57
00:04:30,150 --> 00:04:35,490
to the left by now that we've initialized back, how we want to say while back count.

58
00:04:38,000 --> 00:04:39,950
Is greater than zero.

59
00:04:42,570 --> 00:04:47,460
Then P one is going to minus minus, so we're going to shift P one over.

60
00:04:47,790 --> 00:04:54,960
But remember, we still have to check whether or not this character that P one is pointing to is a hash.

61
00:04:55,800 --> 00:04:59,780
Before we actually check that, we do have to decrement back count as well, because we have moved P

62
00:04:59,790 --> 00:05:00,680
one by one already.

63
00:05:01,170 --> 00:05:04,460
So we have consumed one of these back counts.

64
00:05:04,470 --> 00:05:06,930
So here we can just say back home, minus minus.

65
00:05:07,290 --> 00:05:12,470
Now, is that check to see if the new value that P one is pointed to is also a hash.

66
00:05:13,050 --> 00:05:18,990
So we say if S at P, one is equal to a hash.

67
00:05:26,290 --> 00:05:28,480
Is equal to back count.

68
00:05:35,420 --> 00:05:37,040
And then we close off our while loop.

69
00:05:38,380 --> 00:05:44,620
Now, if you're curious as to why here we just do this check and then inside this block, we do this

70
00:05:44,620 --> 00:05:52,030
check again, it's because when we realize that we're pointing to a hash, we need to move this pointer

71
00:05:52,060 --> 00:05:58,750
until we're technically resolved with moving the pointer due to how many hashes there might be in the

72
00:05:58,750 --> 00:05:59,170
string.

73
00:06:00,180 --> 00:06:06,880
Only if they're within the amount of pointers that we're moving as far as the string is concerned.

74
00:06:06,900 --> 00:06:12,190
So here we can see what this string when P2 moves over to the first hash right here.

75
00:06:12,930 --> 00:06:14,690
There is one more hash to account for.

76
00:06:14,700 --> 00:06:18,090
So we can't just move that pointer to where the final position will be.

77
00:06:18,540 --> 00:06:20,610
Let's say we do account for this one.

78
00:06:20,910 --> 00:06:26,550
From this point on, we need to get to this B, but what happens if the Z or this value right here instead

79
00:06:26,550 --> 00:06:33,960
of being A B, either of these or hashes, then we technically need to still keep moving our pointer

80
00:06:33,960 --> 00:06:37,740
over until we're done removing characters.

81
00:06:38,040 --> 00:06:41,580
And that's what this block of code here represents.

82
00:06:42,090 --> 00:06:43,560
So that's why we do it in this way.

83
00:06:43,920 --> 00:06:51,060
We want to do this all in one move or one block of logic when we first encounter our first hash.

84
00:06:52,990 --> 00:06:56,770
So this is pretty much the logic for this block of P1.

85
00:06:57,700 --> 00:07:02,740
And now what we want to do is we want to do it for pizza as well, and the code is pretty much going

86
00:07:02,740 --> 00:07:03,580
to be the exact same.

87
00:07:04,030 --> 00:07:06,010
So here we just say if.

88
00:07:08,230 --> 00:07:12,070
T a P two equals a hash.

89
00:07:15,840 --> 00:07:21,060
Then we're going to do the exact same thing, we're going to initialize a back count variable here.

90
00:07:22,920 --> 00:07:24,360
And it's going to be equal to two.

91
00:07:25,680 --> 00:07:27,990
And we'll say while backout.

92
00:07:31,280 --> 00:07:32,630
Is greater than zero.

93
00:07:36,400 --> 00:07:43,180
Then we want to decrement P2, so we want to shift it over by one, we want to also decrement backout.

94
00:07:46,220 --> 00:07:51,770
And then we do that same thing, we check if the value at T.

95
00:07:52,800 --> 00:07:55,710
AP two is equal.

96
00:08:02,990 --> 00:08:08,510
Because if it is, then we are going to increase back here.

97
00:08:18,100 --> 00:08:19,780
We close off these functions.

98
00:08:21,100 --> 00:08:26,170
And this is the logic that will decide what happens if we're looking at a hash.

99
00:08:28,040 --> 00:08:33,710
When we are finally done and moving our pointers due to having encountered any number of these hashes

100
00:08:33,710 --> 00:08:41,180
that are grouped together, we then consider what happens now if it is a regular character, which means

101
00:08:41,180 --> 00:08:46,090
that we're at the L section of this if statement right here.

102
00:08:46,520 --> 00:08:48,470
So we need to write the right here.

103
00:08:50,770 --> 00:08:52,510
And here will say else.

104
00:08:53,640 --> 00:08:59,880
We have a character, so this is where we evaluate are these characters equal to each other at P1 and

105
00:08:59,880 --> 00:09:06,150
P2 because we know we've moved our pointers to the right position where the characters now must end

106
00:09:06,150 --> 00:09:07,090
up in the final string.

107
00:09:07,710 --> 00:09:08,970
So we say if.

108
00:09:10,270 --> 00:09:13,900
S at P one does not equal.

109
00:09:14,990 --> 00:09:16,520
Te ap to.

110
00:09:18,400 --> 00:09:21,130
Then we return false because we know for sure.

111
00:09:23,510 --> 00:09:30,020
The two strings are not the same at this character, if they are the same, then we have to check for

112
00:09:30,020 --> 00:09:31,720
the remainder of P one, MP two.

113
00:09:31,730 --> 00:09:35,090
So P one is going to remove or move over by one.

114
00:09:35,140 --> 00:09:37,370
P2 is also going to move over by one.

115
00:09:38,420 --> 00:09:44,220
And then our code is going to run through this entire loop again and check if there's a character or

116
00:09:44,220 --> 00:09:49,310
if it's something else, like a hash, and then it has to do all of that logic above again.

117
00:09:50,000 --> 00:09:56,780
And once we're done evaluating this, we know that if all of these conditions are passed, then we've

118
00:09:56,780 --> 00:09:58,670
reached the very start of our string.

119
00:09:58,670 --> 00:09:59,930
We've compared those characters.

120
00:10:00,290 --> 00:10:08,210
P1, P2 are now no longer in positions in the string because they've moved all the way over, compared

121
00:10:08,210 --> 00:10:10,610
the characters, moved all the way over, compared the characters.

122
00:10:10,610 --> 00:10:14,900
We're done comparing these strings must therefore match and then we return.

123
00:10:14,900 --> 00:10:15,290
True.

124
00:10:19,410 --> 00:10:21,270
And now we have our final code.

125
00:10:22,280 --> 00:10:28,730
It's always good now to just double check for any typos, we want to make sure that we have closed all

126
00:10:28,730 --> 00:10:34,040
of our braces as well, because when we have so many nested conditions and all of these while loops,

127
00:10:34,040 --> 00:10:37,370
it's very easy to miss a brace or have added an extra brace.

128
00:10:38,300 --> 00:10:43,640
So just double check through your code to see if you've misnamed any variables when you are referencing

129
00:10:43,640 --> 00:10:47,460
them or you have any additional Brice's, I'm going to do the same.

130
00:10:48,140 --> 00:10:54,140
So here, let's just see that we have to keep track of the fact that we have these two Brace's on this

131
00:10:54,140 --> 00:10:55,790
while and this is to check for.

132
00:10:56,420 --> 00:10:58,100
But I'm going to check this block right here.

133
00:10:58,110 --> 00:11:05,620
So this if matches this breaks this while matches this breaks this if right here matches this brace.

134
00:11:05,630 --> 00:11:12,380
So this block of code is fine then this if matches this breaks this while matches this one.

135
00:11:12,410 --> 00:11:14,240
This if also matches this one.

136
00:11:14,450 --> 00:11:22,190
So this code is also good in terms of Brace's, this else matches and starts a new bracket so that if

137
00:11:22,190 --> 00:11:25,820
condition that was up here is what that matches for.

138
00:11:27,300 --> 00:11:34,080
Inside, we see that this if matches here, this else matches here, this price matches this else,

139
00:11:34,440 --> 00:11:35,820
but now we suddenly return.

140
00:11:35,820 --> 00:11:36,310
True.

141
00:11:36,330 --> 00:11:40,530
I am indeed missing a bracket for this while loop here.

142
00:11:40,530 --> 00:11:44,000
So I'm just going to add it and this is my mistake.

143
00:11:44,460 --> 00:11:50,820
So I'm just going to break one more closing price and then we're going to return.

144
00:11:50,820 --> 00:11:51,300
True.

145
00:11:54,750 --> 00:12:00,480
And then close off our original function and here we see the importance of double checking for any errors

146
00:12:00,690 --> 00:12:03,420
in an interview, if you make a mistake, that's perfectly fine.

147
00:12:03,430 --> 00:12:09,030
You just want to make sure that you check afterwards and show diligence on making sure your code is

148
00:12:09,030 --> 00:12:09,990
clean and correct.

149
00:12:11,020 --> 00:12:13,750
So with this, we have coded our final solution.

150
00:12:14,610 --> 00:12:19,470
In the next video, I'm going to walk through the space and time complexity of this function as well

151
00:12:19,470 --> 00:12:21,810
as the LICO, just because this video is getting a little long.

152
00:12:22,260 --> 00:12:28,830
But I do want you to double check and see for yourself if you know what the space and time of this code

153
00:12:28,830 --> 00:12:29,080
is.

154
00:12:29,100 --> 00:12:33,360
It's always good to practice these exercises for yourself, but I'm going to do it in the next lesson.

155
00:12:33,720 --> 00:12:34,860
So I'll see you in the next lesson.
