 What's up guys my name is Michael and welcome to my YouTube channel today. You know what we're doing the next problem of Leap code Leap code easy. We are on a leak code easy ground. All right, so basically this is the playlist of Going through all lead code algorithms. All right, so we're today. We're gonna do jewels and stone If you don't know what Lee coated is Lee code is basically a website that helps you do Interview preparations on whatever topic you want to do. So we've this channel is mostly about competitive programming and Algorithms data structures. So we're just gonna do algorithms. All you got to do is create a create an account and on algorithms sort it by difficulty Sort it by difficulty and then go to the problem. So today we're gonna do rules and stones so What is jewels and stones you're given a string J representing the type of stones that are jewels as Representing the type of stones you have each character in S is a type of stone You have you want to know how many of the stones you have are also jewels The letters and J are guaranteed distinct all characters in J and S are letters Letters are case sensitive. So a is considered a different type of stone from uppercase Okay, so lowercase a different the stone of uppercase a so we have a string of So in this example example one J is equal to lowercase a uppercase big a and S is equal to lowercase a to uppercase a and for these lowercase B and This is the outputting three so Basically, we are trying to count how many of these The letters of J are appearing in S. So this is basically what it's so the reason why it's three is because There are there's one lowercase a here In S right the lowcase a and there's two uppercase a That are also in it Right, so there's a reason why it's because J has a lowercase a So that's kind of one and then J also has an uppercase a right So S has two uppercase a's and one lowercase a so that's why it's three so we're just counting the number of the number of characters that are the same in S That are in J. Right. That's basically what we're doing What how do we do this? Well? First of all, we got to go through all the characters in J So We want to keep track of the number of characters in J if it's already visited Right way to do this is using a map Reason why is because I can map every single character in J to a Boolean of It's already visited or not So what I'm gonna do here is I'm gonna create a map Let's just say is visited Right this map is going to have It's gonna map every character to a Boolean Okay So, what are we gonna do? We are going to store Every character that's in J into this map and Then we are going to set it at equal to true So while we go through all the characters in J We are going to set is visited At J of I with the character The character all the characters in J set this to true What is it's gonna do is gonna go through every single character in J map the ones that are visited to true Now we have all that that is true. We want to go through S Okay, and then count how many that are visited already that All the characters in S that are already visited Oh, excuse me all the characters in J that already visited in S So to do that, we're gonna go through every single character So here I'm going through every single character in S if now if it is already visited if the character This is the character. That's not right if it's already visited. So it is visited It's already visited Create a counter We're zero we're gonna increase it because that means it's already visited after this is done. We're gonna return Okay, so do you guys know what I'm doing? I'm going through every single character in J Because we're trying to get all the characters in J that like add up the ones with distinct We want to get all the characters in J That are in S. All right, that's what That's what we're trying to do. So what I'm doing is I'm going through every character in J and I set it as equal to true Then I'm gonna go to every character of S If it's already visited That increase the number of tools that I've looked at. All right, then I'm gonna return From this code see what does okay? Oh Okay, run it again, okay Accepted okay So that is what my solution is to this and I got a 70% Let's see what other people did because If they there's probably a okay, so there's this is a C++ Okay, this is basically what I did except they use an unordered map and oh So they keep track of it already So here what they're doing is they're they're already adding up all the values While you're going through through S So they go through S length And they they add up all the letters that are that already an S and then here they're going through J and Then they just sum up all them the ones that are Hardier so okay, so I could type this up See seems to look a better solution So basically they're going to map every character to an integer and then add that them up Then they're gonna go through J and they're gonna add up all the sum All the other ones are not zero. So that's what they're doing See So I'm gonna type that up because that that solution said it got faster than 100% my solution got faster than 70% But this one is going faster than 100 So Here what they did was they're gonna change this Invisibility what they did was instead of going through J first And they're gonna keep track of athletes The number of characters that asked repeated so the number characters ask you so here's gonna be one is two Yeah, so for every character that keep in fact of the number of ones are here they have a is gonna be one Okay, so you're gonna have to Bees are gonna be four or larger fees now. They're what they did was they're gonna go through it all Interator Yeah I'm gonna go back And type that up. It's fast. It is a fast one. Okay, so it's just an ordered math. Okay, so for every X and J It is visited The total Here That's really strange Yeah, okay, so I don't know I don't know how the solution got a hundred percent But yeah, so that was that's basically how you do Numb jewels in the stones if you want to keep track of the number of the characters are repeated use a map It really it helps a lot because there's gonna be like a one-on-one relation Generally if you have a map, so then that would keep track of this. Yeah, that's jewels and stones rate Come subscribe if you have any issues, please let me know in the comment section below as always peace