 All right, all right, all right today. We are going to go over a spodge question Which I haven't done spodge in a long time and the reason why is because I don't know I Just don't like reading the problems The problem statements are too long But I'll get over that sooner or later Yeah, we're gonna go over the next spodge question. I think it was It's actually 1 30 a.m. Right now and I am Going to go over the spodge question for I go to bed and take a shower and go to bed and Why why is this taking so long? But yeah, okay, hold up Shouldn't be taking this long Okay. All right Okay, so I Think we're gonna go over I'll Ibo phobia Okay, so this was a DP question All right, so he said this guy suffers from ibaphobia. So it's like a fear of palindromes. So You have a string s and you need to add characters To the string to make it a palindrome. So the characters could be added anywhere inside the string so So doctors took this very lightly and appended the reverse of s onto s making a palindrome So you got fft and they would just add the reverse of it So nowadays he's cured of the disease He still wants to continue the game by his rules So now he asked the doctors to insert the minimum number of characters needed to make it in a palindrome So now help the doctors accomplish this task So we have fft and the doctors change it to tfft adding only one character Okay All right, I'm just gonna explain the solution it requires a the similar problem called longest palindrome subsequence and I'll just explain the solution Because I actually don't remember doing this. Okay. All right So first of all, there's a there's something called the longest continuous Longest contiguous Subsequence I believe Yeah, yeah, hold up Let me just go back real quick. So there's something called a longest Yeah, longest palindrome subsequence Longest palindrome subsequence, which is basically the algorithm that you need to solve this question So what I do first first I took read in the string s and then I reversed the string of This s and then I called this reverse string String that is reversed into this longest Common subsequence for palindrome and then I subtracted the number of characters the length of our string by the longest contiguous palindrome so then it would get me the The minimum number of The minimum number of characters needs to add because this this will return the longest common subsequence and To do this This is a two-dimensional DP So here I created an array to that has a two-dimensional DP and What I'm doing is I'm just gonna loop through Basically, I'm looping through through the two-dimensional DP. Okay if this part is if the If we're both at the end of the string right at Both of the end of the string then The common subsequence for the both of them is zero Right because this means that both of the string is empty, right? so I represents like the current index at the first string and J represents the index of the second string So if both of them are like at the end of the string Or the beginning right They're they're basically zero because they're not there's no common But there's no common sequence for it. Okay Okay, now this is a little tricky part. So now if the last character of What you're pointing at For both of these strings are the same, right? So let's say I'm at the last character and they're both the same Then what I'm going to do is I'm going to just increase one because That means that I'm increasing one as the same common character, right? So the remember I'm one I'm getting the longest common subsequence and I'm just increasing one for the same character That's essentially what I'm doing So here our ij is going to equal to the previous count of the previous Previous count for the number of characters that are the same at the previous indexes of the Both of the two strings, right? So I represents remember I represents the Index we're going through of the first string and j represents the index of the character of the second string So if they're both the same then we're just going to increment one to to the previous number of characters at both of them Otherwise What we're going to do is we're just going to take the maximum of the previous characters That we got prior to it So r at i minus one and j represents Comparing the same number of characters of the previous the previous index to all the values of j right of the previous characters that are similar with j and i minus one and then the r at i j minus one represents comparing all of the The previous longest common the previous common number of characters that are the same of j to Of i to j minus one so all the values prior to it Right, and I don't have like a I Don't really have a way to write this down so I Hope you guys could understand. I don't have a touch pad right now Like if we have like, I don't know z I Don't know like what was it t? F t or something. I don't know. That's a Let's actually look at it the sample case So like if you go back and you look at Yeah, fft. So let's say we had like fft Ft Right, so what I'm gonna do is I'm going to pass this into my longest common subsequence longest number Common subsequence and I'm going to reverse it. So I'm going to reverse this string. So it's now t F F and I'm going to find the common subsequence from both of these Okay The the length of it and then I'm going to subtract n minus it Okay, so in in this in this first look code The first case where I is equal to G zero and J is equal to zero So what this means like if this is this is the pointer that I starts Right and This is the pointer that J starts We're just going to go through both of them, right? This is I and the J If we reach zero or the end of the string where it's either it's empty Right, so if I is like Empty like way pass over here, right? Then the common sequence. So if I and J I or J is like empty so right down here Like I'll say Jay reaches here right It's going to be no matter what it's gonna be zero because it's not common, right? So that's what that case represents this part represents if One of the characters are the same so like if if we get to the part where let's say J goes to here Like J goes to F Right, and they're the same characters. Now. I'm going to take the previous Number of characters that are the same from both of them that I stored in my array table I'm just gonna add one because that means that this F is the same as this F otherwise, I'm just going to take Whatever the previous number of characters that are the same in the previous And get the maximum of the previous of I minus one and then J minus one and the reason why for that is um If we stored The previous number of characters that are the same so let's say right here This is like I and this one is I don't know This one's J. So let's say we counted like I don't know two characters are the same Long the same for here to here, right? Well, if we're comparing this this current character with all the values of the previous one J In order to get the maximum of the longest common Subsequence the number of characters are the same. We have to compare like all the previous characters from here compare it with The current J Right. So if we're storing each of these and we're already keeping track of each count We have to store like the previous values Previous values that we did that we kept track of it in our array But the current J of the current character that we're on for the second string and Then so that's where we have to get the maximum right and for the second one It's basically the same thing. We're comparing like all this the Characters that we counted before that we stored in our array otherwise prior to it To the I minus one so like all the values prior to it compared to this so then we get the maximum both of them So that's what this is for so after we store all those values We just return it The end result there and then if once you return the end result you're gonna get the count You get the values that longest common sub sequence that Common sub sequence for the characters. They're the same and you subtract them. So, yeah, that's basically the gist of this problem It's really late. I'm sorry if I sound like I'm rambling, but yeah, hope you guys enjoyed this video very calm subscribe I'll check you guys later. Maybe I'll do more spodge. But yeah, peace