Before we cut out our optimal solution, I want to remind you that we have the final code inside of
our resources folder in some Reppel file.
If you check our resources folder and open it up, you can always use that as a reference while we're
writing this out.
Diving into it here, I've just initialized the function and I've placed the two strings we want as
reference, the first step to our optimal solution is initializing.
These two points are values that point to the last index of the character in the string.
So here we can say, let's p one equal estcourt length minus one, because length minus one is the index
of that last character.
We're going to do the same thing for T and I'm going to call this P to.
And it's going to be equal to tea length minus one.
Now, what we want to do is we want to write some kind of conditional loop that will do the evaluation
and decide on what to do with P1 or P2.
The difference here is that we kind of need code to govern each one's responsibility, even though it's
similar and you'll see what we mean once we actually write it.
But one big thing to remember is that we want to check for false cases first, because we might know
with this character whether or not this function needs to return false, because if these characters
don't match, then we know we return false.
But if they do match, we don't know that we can return.
True.
We have to check the rest of the string first.
So that's why we're going to do a condition check for false when we compare these characters.
So what we're going to say is that while P one.
Is greater than or equal to zero.
So in this case, we want to make sure that P one is within the indices of these strengths, if it's
passed, then technically P one is done being calculated for.
But we also want to say or P two is greater than or equal to zero as well, because we want to make
sure that we're accounting for both these cases.
If there's a chance that one of these strings is significantly longer than the first one, even after
both these strings are transformed.
We want to make sure that when PETA advances, let's say PETA is the longer string, if PETA advances
and P one is already at the end, then what's going to happen is that it should be some kind of comparison
that returns false because p one is done, there's no more characters.
And if T still characters, then of course we return false.
So by wrapping this while condition here, we can see how that will happen a little later as well.
But this is just the condition of how we're going to satisfy to make sure that these two strains are
also equal even in their final forms.
So let's just say that here we're doing this while check, and what we want to do now is we want to
write a condition that checks whether or not P one or P two are pointing to hashes.
So here will say if.
S at P one.
Is equal to a hash.
Or tea.
AP two is also equal to a hash.
What we're going to do is just say for this if condition, if any of these characters point to a hash,
we don't want to compare the characters yet because if any of these compared to a hash, we have to
move these pointers.
We don't want to compare unless we know for certain P1 and P2 are pointing at characters that will definitely
be in the final versions of the strings.
So by writing this if check, we then step in and write our code for both P1 and P2 and here we'll say
if s at P1.
Is equal to a hash.
So this is the code to govern this first one.
We will say, let's back count.
Equals to.
Now, background is going to be how we decide how many units or how many indices we need to move P one
to the left by now that we've initialized back, how we want to say while back count.
Is greater than zero.
Then P one is going to minus minus, so we're going to shift P one over.
But remember, we still have to check whether or not this character that P one is pointing to is a hash.
Before we actually check that, we do have to decrement back count as well, because we have moved P
one by one already.
So we have consumed one of these back counts.
So here we can just say back home, minus minus.
Now, is that check to see if the new value that P one is pointed to is also a hash.
So we say if S at P, one is equal to a hash.
Is equal to back count.
And then we close off our while loop.
Now, if you're curious as to why here we just do this check and then inside this block, we do this
check again, it's because when we realize that we're pointing to a hash, we need to move this pointer
until we're technically resolved with moving the pointer due to how many hashes there might be in the
string.
Only if they're within the amount of pointers that we're moving as far as the string is concerned.
So here we can see what this string when P2 moves over to the first hash right here.
There is one more hash to account for.
So we can't just move that pointer to where the final position will be.
Let's say we do account for this one.
From this point on, we need to get to this B, but what happens if the Z or this value right here instead
of being A B, either of these or hashes, then we technically need to still keep moving our pointer
over until we're done removing characters.
And that's what this block of code here represents.
So that's why we do it in this way.
We want to do this all in one move or one block of logic when we first encounter our first hash.
So this is pretty much the logic for this block of P1.
And now what we want to do is we want to do it for pizza as well, and the code is pretty much going
to be the exact same.
So here we just say if.
T a P two equals a hash.
Then we're going to do the exact same thing, we're going to initialize a back count variable here.
And it's going to be equal to two.
And we'll say while backout.
Is greater than zero.
Then we want to decrement P2, so we want to shift it over by one, we want to also decrement backout.
And then we do that same thing, we check if the value at T.
AP two is equal.
Because if it is, then we are going to increase back here.
We close off these functions.
And this is the logic that will decide what happens if we're looking at a hash.
When we are finally done and moving our pointers due to having encountered any number of these hashes
that are grouped together, we then consider what happens now if it is a regular character, which means
that we're at the L section of this if statement right here.
So we need to write the right here.
And here will say else.
We have a character, so this is where we evaluate are these characters equal to each other at P1 and
P2 because we know we've moved our pointers to the right position where the characters now must end
up in the final string.
So we say if.
S at P one does not equal.
Te ap to.
Then we return false because we know for sure.
The two strings are not the same at this character, if they are the same, then we have to check for
the remainder of P one, MP two.
So P one is going to remove or move over by one.
P2 is also going to move over by one.
And then our code is going to run through this entire loop again and check if there's a character or
if it's something else, like a hash, and then it has to do all of that logic above again.
And once we're done evaluating this, we know that if all of these conditions are passed, then we've
reached the very start of our string.
We've compared those characters.
P1, P2 are now no longer in positions in the string because they've moved all the way over, compared
the characters, moved all the way over, compared the characters.
We're done comparing these strings must therefore match and then we return.
True.
And now we have our final code.
It's always good now to just double check for any typos, we want to make sure that we have closed all
of our braces as well, because when we have so many nested conditions and all of these while loops,
it's very easy to miss a brace or have added an extra brace.
So just double check through your code to see if you've misnamed any variables when you are referencing
them or you have any additional Brice's, I'm going to do the same.
So here, let's just see that we have to keep track of the fact that we have these two Brace's on this
while and this is to check for.
But I'm going to check this block right here.
So this if matches this breaks this while matches this breaks this if right here matches this brace.
So this block of code is fine then this if matches this breaks this while matches this one.
This if also matches this one.
So this code is also good in terms of Brace's, this else matches and starts a new bracket so that if
condition that was up here is what that matches for.
Inside, we see that this if matches here, this else matches here, this price matches this else,
but now we suddenly return.
True.
I am indeed missing a bracket for this while loop here.
So I'm just going to add it and this is my mistake.
So I'm just going to break one more closing price and then we're going to return.
True.
And then close off our original function and here we see the importance of double checking for any errors
in an interview, if you make a mistake, that's perfectly fine.
You just want to make sure that you check afterwards and show diligence on making sure your code is
clean and correct.
So with this, we have coded our final solution.
In the next video, I'm going to walk through the space and time complexity of this function as well
as the LICO, just because this video is getting a little long.
But I do want you to double check and see for yourself if you know what the space and time of this code
is.
It's always good to practice these exercises for yourself, but I'm going to do it in the next lesson.
So I'll see you in the next lesson.