 What's up guys, my name is Michael and welcome to my YouTube channel today. We're going to go over another problem code forces global round eight. We have code forces subsequence. So pretty much I'll just explain the problem. You're given the string code forces, which is like this string and you just want the print out the shortest string that contains at least K code forces subsequences. So what does that mean? Basically, a subsequence is like this. So in this case, you have code forces is awesome, right? Code forces awesome. It contains code forces four times. And these four times are the ones where it has the sub sequences, right? Of code forces. So the first one is just code forces awesome, right? It has one code forces inside code forces awesome. Right. So that's why it's a subsequence like it contains the string code forces. That's what it is. Okay. Another subsequence Of this code forces awesome is this it has code force, right? The just code force in the beginning of the string and it has an S another S at in the middle right in is So that's what makes it a subsequence. Right. The reason why it is a this is a subsequence is because it has it contains a certain number of letters that contains both code force and it has an S. So it contains the string code forces. So that's why it's a sub it has a subsequence of code forces, right? Code force has a code force in the beginning and it has another S in the middle. So that's why it contains it's a subsequence of code forces, right? And this S is a different S from the beginning S that we saw over here. So that's why it's a another subsequence. It has code force and then another S here. And if we look at the third string, right, code force code forces awesome, right? This is the same string, right? That we have in the beginning code forces awesome, right? Like all these are the same string, but we're pointing out the each subsequence, right? So this has a subsequence another subsequence of code forces because it has code force, right? The string code force in the beginning and it has another S. This other S isn't at the middle to the end. Okay, so then now we have the final code forces code forces awesome, right? And it has a code force right here. And it has an ES and ES in the end. So that's the reason why it's a another subsequence code force here and then has an ES. So that's why it has it's another subsequence. It contains the letters of code forces right here. Code forces has code force and then has an ES. So that's why it's another subsequence of the total of code forces. Okay, so it has a code force. And then it has a separate ES. So that's why it's another subsequence of code forces. So that's basically what a subsequence is it has it's basically containing the certain keywords if you mush them together, even though they're in like different parts of the original string, right? So now our job is to print the shortest string of lowercase English letters that contain at least K sub K subsequence of code forces. Okay, so if you look at here. Let's look at the input statements. So if I just put K equal to one, let's say I put K equal to one. What does that mean? That means that that's just the string code forces, right? Because that's only one subsequence. The whole string code forces contains only one time of code forces, right? The string code forces it's itself code forces. Now if I look at here, put three. Why does this code force this code force with three s's contain the three subsequence of code forces. Now the reason why is because it has code force code forces, right? This keyword code forces in the string, which is like code forces, the actual string, right? It's that contains the original word of code forces, but also has code force, right? As a code force, it has another you could pick another another letter from the string s, right? As you could pick another s and you could still make code forces, right? Code forces, right? You have code force and you have another s. So that's why you could still make code forces. That's why it's another sub subsequence of code forces. And then you could also pick the third one, which is code force, the code force, and then you have a final s. Pick the final s, the final s and the code forces. And that's why that's your third subsequence of code forces. That's why if you put three, the shortest sequence that you could have is this. Okay. Like if I input k equal to three, the shortest subsequence I could have of code forces is this with SSS. So you might be wondering, how do I solve this problem? Well, basically a way to solve this problem is actually to repeat the number of letters, certain number of times in this in the right order, right? And same order of code forces. So if I have like code forces like this, right, I have to repeat a letter, right? It could be any letter. But as long as it follows the same order, I'm repeating it. So if I have like code force code forces, right, and I'm adding a separate s at the end, that's going to be another subsequence. Right. If I said add two s's, yeah, that's another subsequence. Now, if I add another two C's in the beginning, that will also be another subsequence. Right. That'll be like, that'll be another three subsequence because I add two C's, add code forces and also have other ones. And if I add like zero like oh, add like another oh, like two O's, that'll be also another subsequence. So basically what we want to do is just repeat the number of times of each letter in this sequence, certain number of times for each letter. And then as a result, be able to print out the final string at the end. So I actually couldn't solve this question in the beginning. So but now I looked at one of another person's code and it starts to make sense to me. So because we're repeating a certain number of letter, like a certain, all the letters like all the letters of code forces right here. All letters of code force code forces right. We're repeating a certain number of times of all the letters of code forces right in order to have a certain number of subsequence, at least case of sequences. So what we could do is we could have a count for the number of times of each letter of code forces. Right. So I could have an array that has 10 letters because code forces has one, two, three, four, five, six, seven, eight, nine, 10 is 10 letters. So I could keep an array that keeps track of the letters that I'm going to add. All right. So we're going to have an array that keeps tracks of the occurrences of the number of times I'm going to repeat for each certain character. And then what am I, what am I going to do? So in here, here's a code. So I keep, we keep a track of count. And we initialize all 10 of these, all 10 of these to have once. Okay. So all 10 of these counts is going to have once because that's the number of times each letter we're going to repeat. Right. Well, okay. Also, we have to read in K, which is the, the number of subsequences we want at least, right? I mean, the number of subsequences, like in the beginning we're asking, we need to return the lowest string that has at least K code forces subsequences. So that's why that's our K. So we have to read in that. So I'm going to keep track of Kerr. And what is Kerr? Kerr is going to represent the times that I'm repeating that were the current subsequence number of subsequences I have in this string of code forces. Right. So the current number of subsequences I have, I want to make sure my Kerr is at least K. So I'm going to repeat the number of subsequences I'm going to have until I reach the certain number in the end. Right. I'm going to repeat that number of times of the subsequences I'm going to have. So I want at least K subsequences. So that's why we're going to do a while Kerr is less than K. And we're going to certain, we're going to increment our number of subsequences we have. Well, as we repeat every single character. So sorry, guys, there's like a mess. I have messenger on my bed. But yeah, that's what we're going to do. Okay. So what do we do? We also have a position which keeps tracks of the current position. I'm at that I'm going to repeat for a certain number of times in the array of characters of code forces. So into position is going to start at zero. And that's just going to keep track of the current position I'm on. So like, if I go back to here, code forces, right? And I'm going to repeat C a certain number of times, right? I'm going to start my position that I'm going to repeat at zero, right? That's my position. See, I'm going to repeat C number of times, repeat O number of times, repeat D number of times, yada yada until I get to S. And I want to make sure that my number of subsequences is at least K, right? Of subsequence of code forces at least K. So that's what I'm going to do. The position is just representing the current character, the position of the character in our array, right? That we're going through the current position of code forces that we're going through. So code forces, yeah, that's the current position is going to start C O D E F O R C E S. And it's going to come back to the beginning if we need to repeat that number of times again, right? That's what we're going to do. So yeah, that's our position. And while this is occurring, I'm going to have position is equal to position plus one mod by 10. And what does this do? This is just going to increment the position every single time in this while in this while, right? So this is just going to go to the next character that we're at. So if I'm at C, right? After I repeat a certain number of times, I'm going to go to O, right? And a certain number of times D go to D and then yada yada. When I get to S, I need to go back to C again, right? So what am I going to do? I'm going to mod it by 10. So that's what this does position equal position plus one mod by 10, right? When you do position plus one, you are basically just going to the next position of the of the character that we were currently on. So code force is plus one is going to go to the next character that I'm currently repeating. And if I mod by 10, so like, let's say I'm at the end, I plus one. So let's say I'm at 11 plus one. So this is that the last character is 10. I'm going to add one is 11. 11 mod by 10, right? 11 mod by 10 is going to go back to the beginning. So this basically allows me to go back to go back to the beginning. If I'm done repeating the number of times of S, then and I need to repeat more times. So that's why I'm going to mod by 10 and I'll go back to C and I will repeat number of times of C characters that we need. So that's what this this position equal position plus one mod by 10 is. Okay, so now here's the tricky part. This is a little tricky part to grasp. Okay, so current is going to represent the number of sub sequences that we have in this that we're going to have after repeating a number of times of each character. Okay, so if I were to have current current, right? If I want to pretty much if if I'm going to find out the number of remaining characters I need to keep repeating, I have to divide by the current position that I've just repeated on, right? So remember count count 10 is the number of times of letters currently in the array, right? So here we remember in the beginning we initialize count to have ones. So that means that originally it's just all code forces. All the characters of code forces is going to be one, like one. That means the number of times of each letter of code force is going to be one, one, one, one, one, one, right? Because that's just the number of times. Well, if I'm going to repeat a number of sub sequences, I need to divide by the current count of each single number, right? That's going to I have to do that for every single letter because that's going to get me the remaining number of times of to count, right? Of the sub sequences, like if I divide by, let's say I have code forces and I divide by like, let's say I have, I don't know, let's say, let's say we want K to be three. Yeah, okay, let's say one K to be three, like here, here, and I want to repeat S three times. Well, in order to understand the how many I need to repeat, I have to divide by the count of the current count that I have for the letter. So if I want three sub sequences, right? And I need to repeat like S, right? Like I need to repeat S three times in order to have three, three sub sequences of code forces, right? Because remember, we explained that in the beginning. So currently, S would have a number of occurrence of one, right? It's going to have one. And then if I take the current count of each one and divide by that number of times, that's going to be the remaining number of sub sequences to have, right? So we have to divide. Current divide equal to count of position. Okay, so now after I divide current, which is like a number of sub sequence I have, I need to increment the number of times of each character is repeating. So that's what count position plus plus is for. We need to increment the number of times each, each letter is repeating. So if S, let's say there's one S, right? And then now I add another S, well, the number of the count of the times S repeated is now two, right? So that's what we have that, right? So now it's the S, so now it would be the count of S would have D2. That's what we have. That's why we need to increment count position plus plus. Now, I need to update the number of sub sequences I have. Okay, so the reason why we do this is like, let's say we have, let's say we have S is one and we repeated S three times, right? Well, how many subsequences do I have at the moment? So currently we started out with current is equal to one, right? And if I repeated S three times, that means that my new number of subsequences is going to be the current number of subsequences multiplied by the number of times I repeated a certain character. So if I had, remember if I had code forces, code force, now I have code forces, right? Code forces, now I need to have, and I repeat the S three times. I have three different subsequences of code forces, right? Because we have code force with one S, then code force with the middle S, and then code force with the last S, right? So that's three times the number of subsequences I have, because that's the number of times I repeated a certain letter, right? So that's why we have to do that. We have to do current times equal to count a position. And yeah, that after a certain number of iterations, after this occurs, right? And then we increase the position of the times that we're counting, and it comes back to the top where it's going to keep going and keep going. And then while the current number of subsequences less than K, when it's greater or equal to K, this while loop would be done. Okay, and then we finish of the number of times the count should be at. So after this certain number of times this occurs, we are going to have an array of number of times for each character that we have to repeat, right? So we didn't actually, we didn't repeat it any character, we didn't add anything to the string yet. This is just counting the number of times we had to repeat the letters, each letter of code forces in order to make sure our number of subsequences less than K. It's greater than or equal to K is at least K, right? So that's what that's what this while loop did, right? So now we have to actually print out each, every single character of code forces, right? So to do that, we have a string s and that's first we start out with code forces, right? That's original string, right? And now whatever we're going to do, we're going to go through every single character inside code forces. So we start i equals zero, i is less than 10, i plus plus, and this is going to go through every single character of code forces, right? Now what are we going to do? We are going to repeat certain number of using our count array. Remember we had a count, this count array that we updated every single time, this count array basically gave us the number of times each letter we need to repeat, right? So after we have the count array, all we have to do is just repeat that certain number letter that many times. So this for loop is going to repeat C, let's say I have C, right? It's going to start at C and it's going to repeat number of times that C occurred in our count array, right? That's what it's going to do. Then it's going to take O and it's going to repeat the number of times O that we need to repeat in order to get K subsequences. And it repeats D-E-F-C-O-R-C-E-S, the summer times of our count array. And then what are we going to do? We're going to print out the current letter that you're repeating at, which is S at i, which is S at i, which is S at a certain letter. And we repeat that number of times of count. So yeah, after that we're pretty much done and then we just print out a new line. So that's pretty much how you do this problem of code forces subsequences. You have to keep track of the number of times each letter you're repeating. And then once you get at least K subsequences, you're done pretty much. So yeah, I hope you guys enjoyed this video. Rate, comment, subscribe. I'll check you guys later.