Given a string X and a target string S, find the minimum number of operations to make S appear as a substring in X, where each operation appends the current value of X to itself. The solution involves expanding X by repeatedly appending it to itself until its length is at least equal to S, then checking if S exists as a substring. The key insight is that S must be a repeating sequence of X, and the number of operations equals the ceiling of (length of S divided by length of X).
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
9AM in Florence, Italy (Doing Codeforces)Added:
Reporting live from Florence, Italy.
This is the birthplace of the Renaissance and one of probably the most artistic city I've ever been to. It really looks like they prioritize artists and fund them and encourage it.
This is also the duomo. This is the y'all played Assassin's Creed.
Let me show So you can see this is this church is actually amazing. This church is one of the most beautiful churches I've ever seen.
>> And then if you ever played Assassin's Creed that's the that's the dome.
But you can see this place is just lined up with artists.
Artists everywhere around the city art. There's a lot of really good museums.
There's a lot of good street singing and dancing. This place is a very fun city.
So, so far I visited Athens.
I've visited Athens. I've visited Hold up.
So, y'all can see me code while while the dome is in the background.
But yeah, this city really prioritizes art. You can tell it's a beautiful city. artists everywhere. It's hot. It's 32 degrees Celsius. It gets up to 34, 35. I mean, the food is good. It's busy. This This church is beautiful as well. Like, I went to Barcelona and I saw all the the nice churches there for sure. They were nice, but I really like the Italian style. Also, even with Italian art, you could tell or you could see a difference between the Italian art and all the other art I've seen in in Spain, France, etc. It's definitely different. I noticed it right away. And I actually really like Italian art. This church is one of the most beautiful churches I ever see. Probably the second next to the the one in Spain.
But yeah, so far I've been to Athens. I was in Venice.
I was in Santorini.
Now Florence and I'm about to head to Rome in a couple hours. But Florence has to be top two. Venice and Florence is Yeah, those are my top two cities right now. But I mean, y'all came here for some leak code, so I got some cappuccino.
I got cappu ice cappuccino this time for real. not a espresso. But yeah, definitely would recommend Florence to anybody visiting Italy. This place is beautiful and it motivated me a lot. Um, but if you're in art or anything like that, this is the place to be. You'll get a lot of inspiration. Even though I'm not even an art, not really an artist, still was very inspired visiting here. So, I mean, let's just run some people real quick.
recording.
Probably do like a 20 to 25 minute session.
I removed my background noise filter so y'all can just hear the city cuz um hear the sounds of the city. Everyone's trying to get into the cathedral. But look how nice this cathedral is.
>> Very beautiful.
restaurants.
It's like streets are busy. It's 9:00 a.m. in the morning.
Look at this church. I don't know if this is the best angle. I don't know if y'all think this is looking nice, too.
But yeah, Satis Creek Dome.
Such a beautiful city. Definitely would come back.
definitely would come back here and and and just spend a month doing code forces and trying to get the code forces grandmaster from here. I think this is like a optimal setup.
Get the dome in the back. I'm getting saved by this tree. It's really hard.
But let's let's grant some questions real quick.
I also ordered a chocolate croissant.
So, I'm just waiting on that.
So, given a string X of length N and a string S of length M consisting of lowerase L letters, you can apply any number of operations to the string X.
In one operation, you can append the current value of x to the end of the string x. Note that the value of x will change after this.
Given this given a string x of length n and a string s of length m, you can append the current value of x to the end of the string x. Note that the value of x will change after this. For example, if ABA then after applying X operations, you'll get ABA. AB Okay, after what minimum number of operations S will that number of operations S will appear in After what minimum number of operations S will appear in X as a string as a substring? A continuous substring of a string is defined as continuous segment of it.
negative. You return negative one if it's impossible.
>> Okay, >> now let's take a look. So, this one, let's put this to the side.
And I love just hitting random cities and doing code courses.
>> I wish I could make this a full-time job. This Um, there you go.
So, we're given a So what's the input?
I think we're given the first line contains n and m. The second line is x and second is s. How many times can we append x to itself to get s? I mean this case it's just five. The output should be five or three I guess cuz in the first operation go a then a then we add a a again loud but I I got to I got to leave it running the so y'all can hear what the city is like. So now S is in there. So it's three.
One, two, three. I mean the brute force force would just be to try to pen and check the substrate.
>> When will it not work? Let's look at the We have to look at all edge cases. when it would not work.
So in this case, we get X.
I noticed that's one thing that that's going to help me move faster cuz I was watching some of my film and I realized that if I do both CA if I do the the passing case and the failure case, I could probably get to the solution a lot faster.
AB AB AV one. Two.
>> How does this not work?
>> That should be one, two.
signing.
>> Okay. When does it not work?
>> Yeah.
Yeah, it it just becomes an extension.
What is like X Y X Y Z XYZ?
left cuz you so this you just need to see you you need you basically need to you basically need to see if X is a repeating sequence of S. See if S is a repeating sequence of X.
So, what I would do is probably just take X and expand it out until it's greater than or equal to and then check if it exists.
Let me try to get the Wi-Fi to see if this this works. All right, connected to Wi-Fi so I could actually run my test cases. But what we'll do is we're just going to expand out. So, pretend this is X. We'll just expand it out until it's greater than or equal to the size of A.
And then we'll just see if S is in X.
So we can do just a string builder. So that'll be o. So we're going to do is we're going to let's let's copy our input from one of the other ones.
Going to go ask for my croissant soon.
So what we're going to do is this is the number of tests and then for tests we're going to get the input cuz we don't really care about the first one. For the second one we're going to get S and then we're also going to get X or we're going to get X and we're going to get S.
We got enough. So what we're going to do is um while length s is greater than length x what we're going to do is x plus = x if s if x's easy one See?
>> Oh, we need to print the number of times.
I think we we can do something like this.
What happened here?
Let me just make sure I'm capturing the input correctly.
Let's make sure we're capturing the input correctly. A at the cost.
>> What funny Three.
Okay, that work.
Oh, wait. Did I read it wrong?
Got the back machine.
Wow.
this edge case cuz we just add force and then it gets there after.
>> So just that would just be zero right here.
Let's see if this Let's see if this brute force just works.
Okay, let's work to this example. What if I I didn't see this edge case, but what if we start off with E4s and then our job is needs to be at least four. It's one hit.
texture tickets.
>> Maybe we could just Is there a time that we can get Okay, it's not that easy as I thought.
Where disappears.
Well, okay. This the insight. It can be two. It can be it could be rotated and still work. So, what's the rotated case? So, if we have XYZ, right?
This is so at ts S X is so S is target. X needs to be uh repeating repeating substring.
We should be able to partition S with X.
I think we can just brute force this. So what we're going to do is while the length of S is greater than equal to as if it's some sort of rotating X.
So say x = y zx and s is equal to xyz xyz.
So once we take x and we go that could eventually equal.
>> So if it's rotated, it'll equal we I think we got to rotate it. Do we have to rotate it or just just keep trying?
start.
>> Okay, I think we just need to capture the two cases. But what we'll do is we'll just brute force this. If the length is greater than if the length of x So we'll do is if then we need to be able to break while S is in X three.
Something like this.
21gative one.
Why did this kiss? One, two, three.
One, two, three, four.
There's a mismatch here.
One, two, three, four, five.
I feel like we're approaching this question wrong.
We're approaching this question wrong.
Okay. What's this case?
My nose is runny and still a little bit sick.
Okay. What's this case? We have X.
Thank you.
How do we handle this case?
Feel like it needs to just be some rotation. It needs to be.
Yeah, this this case breaks cuz S is already greater than but actually it can start off greater than and then you could add it. So I think you just need to if it's greater than And finally they do this.
>> He got So, I'm like >> going to try this out and then we're going to turn three.
How am I getting folded by this?
Okay, how let's let's try to look at this from a different perspective. I feel like it needs to be like some rotation.
Just one case, too.
>> I feel like we need to do one case.
forces.
>> Yes.
If temp equals If temp equals If temp equals Okay.
Oh, how do we do this?
What the heck?
Good bro.
Know what result is this?
312 one zero almost.
What?
That was just not worth supposed to go here.
How am I getting folded?
Embarrassing myself over here.
>> Yeah, I'm not doing this correctly.
always.
Okay. Damn, I only got to one question.
Okay, we're going to fight through going to fight through what is abstring at this.
>> So there's X 1 2 3 4 5. 1 2 5.
I >> feel like you just have to keep expanding it for like some number of iterations.
Maybe like let's just go. Let's just restart length x.
How much times does x go into s and take the ceiling of that?
For this case, one goes into two twice plus one.
One, two, where we're going to take S.
X. So >> how much times can S be divided by X?
So if it's six, then we take the seal of that seal plus one.
question.
scene. I looked at this completely wrong. So, chat, let me know how y'all would solve this. Scene equals true.
This just straight. I tried to maybe overoptimize it.
probably tried to over optimize it, but okay, let's admit this.
>> All right, let's admit this.
>> Hopefully I don't TLE.
>> Hopefully I don't TLE.
But TLE, I'm calling it. It's all good.
All right, we got to look at this a different way.
I got to I got to catch my How do we look at this?
We're looking at this completely wrong though.
What is this question?
Yeah, we're looking at this completely wrong.
>> Or maybe this is what one of those ones that you could only solve using C++ cuz the string manipulation.
It's all ash is always not greater than five. Why wouldn't >> I would not be able to solve that.
I I wouldn't have came up with that to just run it on five. I wonder what test case I'm I was breaking on.
Now you This is why a lot of code forces people really look at the constraints.
Huh.
I don't know if I'd be able to I would have been able to get that.
See, accepted.
Holy that that question put us through it. But look how beautiful this city is.
One last time for you. Look at all the artists lined up.
>> All right.
All right, going to head to Rome and I'm going to do this again in Rome. But thanks for tuning in. Catch y'all in the next video. Peace.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











