Welcome back.
Let's now get into the code for solving the longest palindromic substring question.
So the code that I have over here is the same thing that we have written for the tabulation approach
for the palindromic substrings question, which is something that we have previously discussed.
Now, as we have discussed in the previous video, we would just need to make a slight tweak over here
to modify that code to be able to solve this particular question.
Okay.
So all that we need to do is over here okay.
For every I j combination we just need to check whether depee at I j is a palindrome.
And if it is a palindrome then we will just have a variable.
Let's say we call it longest and we have to initialize it over here.
So let's say net longest be an empty string okay.
And over here we will say longest is equal to s dot slice I2J plus one okay.
So I to j because we just need to take this substring over here which is represented by depee at I j.
And we need to assign it to longest.
Now why does this work.
The reason for that is because we are iterating through the DP table in a length wise manner.
Whenever we get a new DP at I j to be true, the length of that particular substring would be either
equal to or greater than the previously true dp at I, j right, or substring, and therefore we just
need to keep doing that.
And ultimately whatever value is there in longest would just need to be returned so we can return it
over here.
So return longest.
And that's it.
It should solve this question.
Now let's go ahead and run this code and see if it's passing all the test cases.
All right.
It's working.
You can see it's passing all the test cases do make use of the user logs.
It will help you debug your code in case you're stuck at some point.