Welcome back.
Now this is the recursive approach that we have coded in the previous video.
Now let's go ahead and memorize this.
And for this we will need a DP array.
Let's just call it DP itself.
And this is going to be an array of size n.
And we will fill every cell in this array over here with the value minus one.
Okay.
Now in the Czech ending at function over here after checking the base case, before we go ahead and
do these computations over here, we will check if depee at index is not equal to minus one, because
if it's not minus one, the reason for this would be that we have already previously calculated and
stored it over here.
Because notice initially we had filled every cell in the DP array with the value minus one.
Okay.
So if this is true then we can just return dp at index.
And we do not need to do any of these computations.
But if that is not the case, then we come over here and we do all of this.
And before we return true we are going to store it.
Okay.
So I can say DP at index is equal to true.
And then we can return either dp at index or true.
It does not matter okay.
But let me just change this to return dp at index.
Now over here we have false.
And again before returning false we have to store it.
So I can say dp at index is equal to false.
And then we can just return dp at index okay.
So that's it.
So just by adding a few lines of code we were able to memorize this.
Now let's go ahead and run this code and see if it's passing all the test cases.
And you can see that it's working.
It's passing all the test cases.
So this is the memoization approach for solving the word break question.