 What's up guys, my name is Michael and welcome to my YouTube channel today. We are going to go over string LCM Which is the second part of part B problem B of the educational code forces round 102 so You define a multiplication operation between string a and positive integer x where a times x is a result of Writing x a time so what they mean by that is like let's say I have a b times 2 so like if I had a b C right times 2 what they mean when they say times 2 the operation means you just concatenate ABC twice Right, so you would have ABC and then ABC again Right, that's what they mean by this so if I had like um If I had another another thing like I don't know KDZ or something right now times by 3 What that means is that I just write KDZ three times so I already KDZ KDZ and then KDZ Right, that's what this operation means So um, yeah, and then if you add a times five you just write a five times So you say a string a is divisible by another string if they exist x and your x where you couldn't when you Do b times a x you get a so what they mean by is that? Here's what they mean by this so like if I had like a b a b a b right and have a string a b They say the this string is divisible by this string and the reason why is that if you Take any number right, and then you take this this bottom string, right? You take a number and you multiply it by a three if you take a b You will multiply by three you get the first string right because and the reason why if you take a b a multiple by three reason why you get the first string is because the the sec the first string is Equal to the second string Concatenated by itself three times so like if I take a b Multiply by three and I concatenate by itself I would get a b a b a b and this string is exactly the same thing as this string, right? So that's why they say it's divisible by each other. So this is just their definitions. They're giving right so if you String a is divisible by another string b there exists an integer x such that a b times x is equal to a so that's what they mean by like a b a b a b is divisible by a b But it's not divisible by a a right. So like um If I go to go back to a b a b a b a b a b, right? so if I take Any string a b and I concatenate it three times I would get this string, right? but uh this string and this string a a is not divisible by each other and The reason why is because I cannot like take a a and concatenate it Added a concatenated by itself three times. It's not gonna get me a b a b a b, right? Like if I no matter how many times I try to append a a to itself like I do a a Appended by itself like twice a a it's never gonna get me to a b a b a b So that's why these two are not divisible not divisible. I shouldn't I should not read not equal not divisible Right, these two are not divisible. So that's what that means, okay? All right, LCM of two strings s and t is defined by LCM, which is the shortest non-empty string that is divisible by both s and t so shortest non-empty string that is divisible by both s and d and what that means is that the let's say I have Basically what they mean by the LCM of two is that if you have two strings It is going to be the shortest string that is divisible by both of them, right? So that is if you concatenate it that many times you will get both of them. So like uh in the case Let's go over the first two examples. If we have b a b a and b a So if we had let's say s is equal to what was it b b a b a right this string and then I have t is equal to b a and if I do LCM of s and t I should be able to get The shortest string that is divisible by both of these is going to be b a b a Okay, and the reason why is that if I were to take t and I concatenate it by itself Twice I would get s right. So if I take t So if I take t, which is b a and I multiply it twice It's gonna give me the same answer as s Which is a b a b a Right this s multiply by once right, so the LCM of S and t is going to be b a b a right because b a b a is these These common multiple that is divisible by both of them. Okay, right these common multiple Okay, um, yeah And we could go over a what is the next one a a a a right? So if we go over the next one, which is a a and a a a let's go over that one. So a a is s and Then t is a a a I think is that what they said Yeah, a a a right if you were to concatenate do do an operation of a a a and a a a You're gonna realize that the LCM of both of these is going to be a Concatenated six with six a's Yeah, that's what it's gonna be and the reason why is because if I take s right and I multiply s by like three times So I take s Yeah, as which is s is a a so yeah, I'll just do s multiply by three times That's gonna give me t Multiply by two times right so like if and the reason why is because if I take a multi a a multiply by three times I'm gonna get so like a a Three times and a t is a a a a a multiply by twice I'm going to get a a a and three of a a's which is a a a a a And that's gonna equal to a a a a which is a a a a a a right So this is um the least common multiple string would be this a a a a a a a because it's the least common as divisible by both these a a and a a a right So that's the reason why that's the case and if you were looked at look at the last statement be a b and Yeah, a b a and a b a b. So if we go back we get a b a and then We check a b T is equal to a b This is gonna give return negative one because no matter how many times you concatenate s by a number and t by a number you're not gonna get the same string right and Yeah, you would get a negative one and the reason why it's cuz like if I take S and concatenate let's say I'd take s and I concatenate it. I don't know by once right by once And then I take t and I concatenate it by twice so t a b concatenate it by twice I'm going to get a b a b right, but then that's not gonna equal to a b a Right, no matter no matter what and the reason why it won't work is because like s has an extra a at the end So no matter how many times you concatenate it. It's not gonna work for you Okay So, um, how did I do this? So now how do you solve this problem? Basically what what I did was I took the length The LCM of the basically took the length of both of them because that's gonna be the number of times you would do so what I did was like Let's say I had a let's go back to the first one Yeah, b a b a and b a b a b a and then t is B a yeah b a so if I take this first one right We take the length of this this length is four Because it's four characters one two three four and then if you take the length of this Which is b a which is two right so um, if if you were to think compare with s and t One of these numbers has you have to concatenate a certain number of times for each of these numbers in order to get them to To the same number right that you have to repeat a certain number x number times or t y number y number of times right so um Basically, we're trying to get So basically here, I'll show you what I mean like if we have s we have to concatenate it by like x number of times that's gonna give us t which is This is t b a and we have to concatenate this like y number of times right so we repeat x We repeat b a b a x number times repeat b a y number times. They have to be equal to each other right So what you could do is you could if you realize that um, they're gonna have to be equal to each other in the end That's actually just the least common multiple of the lengths right of four and two Right, so if you find the least common multiple the lengths four and with the four or two That's going to be four right because LCM of Four and two is gonna give you four right and the reason why is because of four times by one is equal to Two times by two right So if you were to repeat if you find the LCM of the lengths of both four and two You would get you would basically get This right the LCM of which is four four So the number that's going to be repeated x and y right the number of times that each string gets repeated It's just going to be the the total LCM divided by the original number of the length of the string Right and I could I could explain it even more again. So like if I take um Well, let's actually go back so remember LCM of Four two is two right Um, and then if you take four times i one you're gonna get two times i two so in this case x would be equal to one because you're just repeating one uh, b a b a one time And y would equal to two Right, this is going to y was equal to two and x this x is equal to one because you're and this is because you repeat Y is equal to two because you repeat b a twice Right, you repeat b a twice So what you could do is you basically just take the LCM of the lengths right Then you divide the the LCM of the lengths by each individual Each individual length of the string and then you repeat that that number of times And then you check if they're equal to each other right because if If you were to repeat it that number of times and they're equal to each other That means that you could definitely you could basically are divisible. That would be the least common multiple of the string So, um, yeah, let's actually go over the example of b a b a and b a Again, so like let's say I so here b a b a and t is equal to b a Right, so the LCM of both of these so this length is four. This is two LCM of four and two is two Oh, no two four LCM of four and two is four right, so that means that basically four is going to be the length of the End of string that we want right at the end the the length of the string in the end that we want, right? because that that's what's going to be and Now what i'm going to do is i'm going to take this four And i'm going to divide it by the length of s so the length of s is four, right? So i'm going to take four divided by four It gives me one and i'm going to concatenate s by itself one time So i'm going to compute s times one and that's going to be b a b a times by one which is going to get you b a b a right That's what's what's going to get you then now for b a LCM of both of these two lengths is four right four divided by two is two Right four divided by two is two Right four divided by two is two so i'm going to concatenate b a twice So here i'm going to have b a Let's actually yeah b a here i'm going to multiply by two So i'm going to get b a b a right And then so because i have These strings now all i have to do is just check If these strings are equal so like here For t for t uh t multiplied by two is going to be b a b a s multiplied by one is going to be b a b a Therefore the end of the string they're both equal and the answer would be b a b a If i can't multiply them together then i just print negative one And we could actually go go over an example of that So here we could do b uh we could try the last example a b a and a b So the last example is a b a and a b right so this is s And this is t Okay, so The lcm of this length and this length so s's length is three because there's three characters t's length is Two because there's two characters so the lcm of s and t right least common multiple is going to be six Right because three times two six right So the total length of the lot end of the string is going to be six So our total length End of the string is six So now i'm going what i'm going to do is i'm going to take six divided by three Which gets me two so six over three is two And i'm going to repeat s Times by two Right concatenate s by itself two times. So that's going to give me a b a multiply by two Which is going to give me a b a a b a okay, so that's My last string of s Now let's look at t t uh t's length is two Remember our length of our final string is six six divided by two is three So what i'm going to do is i'm going to concatenate t three times so t times by three is going to be b Times by three which is going to give me a b a b a a b Okay, so now because i know that both of these are This is going to be the end of the string assuming i get the lcm Both of them right and s is the end of the string would be a b a a b a All i have to do is check if these two are equal so now um s and t Are they equal so we have a b a b a b and a b a b a There's an extra a here. So it's not equal right, so it's not equal These are not equal. So therefore this is impossible. So I would print negative one And we could just go over the code that I wrote Now let's actually just go over the code. It's actually not that difficult Um, yeah, so here I take the lengths. Uh, read in the string s and w. Oh, it should be t but Uh, whatever Whatever It doesn't matter. Okay. Um, here I get the lengths of all them because I don't feel like writing dot size over and over again So here n is the length of the string s and m is the length of the string w And then I get the lcm of both the lengths right v I had to get the lcm calling the lcm function and that's going to get me the length of the final string That we have in the end right l is equal to six right Like the final string that we have in the end then what I do is I say, okay, I'm going to create a new string for both each of these And for that new string, I'm going to concatenate that number of times. Okay, so here s The new string of s is going to be like a b a and I'm just going to concatenate it that by three times You know, these are the two new strings that we're going to check So here I'm going to hear what I do is I do okay K is equal to s e is equal to w. So these are two new strings and then I'm just going to loop through over them Okay, um, I start with one and I'm going to loop through a v over n times. So v is the lcm both of them Uh, I was kind of rushing when I was typing this because I wanted to submit fast. So Yeah, I just used v but um, it's better readability if you just use lcm and m But yeah, this is just taking the lcm the length that we had and then dividing it by Each of the lengths of the string And I'll tell you then how many times you have to concatenate by the number. So that's what I do there. Hey there, okay Or I also have to check if there it's greater than one because if it's not greater than one You can't really concatenate that many number of times, right? Because if it's zero, you don't really want to concatenate it So yeah, um, and then I do i plus plus and then I basically just concatenate with the string by itself So k plus equal to s and here I do the same thing with the other string of e right This is the second string them concatenate it and I concatenate it that many number times Okay, and in the end I just check if k is equal equal to e Which means if they're this equal to each other and if that's the true I just print k otherwise I'd print negative one So yeah, that's pretty much the gist of it. The code isn't that difficult um, I was pretty surprised that I actually solved this problem because I don't normally bees problem bees are really difficult To me problem bees are like really really difficult, but I was very surprised that I solved this problem But yeah, that's pretty much the gist of how to solve this question So I could repeat myself again. Um get the lengths of the both of the string right of s and t of both of the strings Call lcm on them and get the least common multiple both of the strings That'll be the the total length of the end of the string Take this total length and divide it by each of the individual lengths of each of the individual lengths of the string And that will give you how many times you're going to concatenate it The individual string right so here it would be six divided by three would get you two So that would be the number of times Concatenate s by itself two times right and here it would be six divided by two which gives you three and you concatenate t that number of times Right, so once you find that number just concatenate it with itself that number of times And then just check if they're equal To each other right and then if that's the case then you're done. You're pretty much done So yeah, that's how you do this problem Rape comp subscribe. I'll check you guys later. I'm really tired. Peace