 What's up guys, my name is Michael and welcome to my YouTube channel today we are going to go over sparse arrays. So basically there's a collection of strings input strings any query of query strings. So for each query determine how many times it occurs in the list of input strings and then return an array of the results. So that's the problem. So the example is that let's say we have this array of strings a b a b a b c right and then we have queries a b a b c b c. So basically, we want to return all the queries reach query in here, we want to return the number of times this this string appeared in the array. So here we have queries a b should return to two times because a b appeared twice in the array of strings, right, we have one here and then one here. Then we have a b c and a b c appeared once in here, right appeared once in our array of strings, we have one a b c and in the array of strings. And then b c has zero because there's no zero there's zeros, there's nothing in here in the strings. Okay, so I'm going to explain how to do this problem. So what I'm going to do is I am not actually going to use any other library besides arrays. And the reason why is that if you're not going to like because this problem type is arrays, so I'm not going to use maps, and I'm not going to use sets. But there are really easier ways to do this if you use a map, hash map or a set, hash set, right, you could always do something really easy, and you could solve this problem really easily. But I'm going to do this using only arrays. Okay, so to do this problem using only arrays, what you're going to do is you're going to have another array called the count. Right. And this array is going to for each element in your strings, it's going to loop through all the values in here and count how many times it actually appeared in the string. So let's say we have AB here, right, this count array at this index of AB of zero is going to store how many times AB appeared in this array. So it's going to sort to, it's going to loop through all the values in this array, and then it's going to count how many times it's going to hear it's going to have two. Now in the second column here, AB also, we're going to count how many times disappeared in the array also, and it's going to be two as well, because there's two, two ABs here, so we're just going to have two here. And then ABC is going to have number of times it appeared in this array, which is it's going to loop through, and it's just going to have one. Okay, so now we have two to one, okay, and in our, in our strings, it's going to have two to one. So then now what we're going to do is we're going to look through queries. And then for each query, what we're going to do is we're just going to loop through the strings and check, does this query equal to any of the elements in this and in the strings. So what we're going to do is we're going to look through queries and see, okay, there's AB. Now, is there an AB in my array of strings? If there is, then I'll just go check the what print out the count that it has. Right, so here at AB appear twice, right, because there we have our count array. So it's just going to print out two. And then we have ABC, right, we have ABC, it's going to loop through the strings and check, okay, does ABC is our queries at ABC equal to ABC. So if it does, okay, it's just going to print out one, the count of one. And I'll print that that out. And then we have BC, it's going to loop through all the strings of here, check does it equal to BC? There's none of them. So it just, it's going to just return zero. Okay. So that's basically the gist of it. It's actually not much, not much, not much to do. So I'll just explain the code now. So here, I know it's not efficient, but it's actually this is actually, I think this is how they want us to solve the problem. Otherwise, it wouldn't make any sense for it wouldn't make any sense for them to have this problem in arrays. Alright, so here I have a the array of count, we're just going to be total, I call it total, you can count whatever it you could have it as whatever, it's going to equal to the size of the strings. So here we have strings, it's going to equal to the size of that. Then I have a array of results. So this results is good, just going to store the result for each query, right? So we're looping through the queries and then see how it does DE what is the result for DE print that what is the result of I am and I'll print that what is the result of FGN I'll print that. Okay. So what do I do first? I loop through my strings of all the strings and strings. And I loop through my strings again. Okay, so I'm going to start from zero and go to the length of strings, I'm going to loop through it again from zero to length of the strings. And each time I'm going to keep track of the count and I'm going to increment count the number of times it looks it sees this string, right? So then basically what I'm doing is I look at AB, right? I'm going to then I'm going to loop through AB again. So look, I see AB, right? Then I'm going to look through the strings and count how many times AB appears in this strings. So does this equal to AB? Yes. Plus one, does this equal to AB? Yes, plus one, does it equal to AB? No. So our count will be two. And then I'll do the same thing for the rest. Right. So that's what this is. So I look through in the beginning, I go to the end, I check if it's equal to strings j, then a countless plus. And then, then I just set my total, which is my total array of count is going to equal to count, right, the number of times it appeared. So maybe we should call this like, let's see. It's instead of total, let's call it count, actually, count here, let's just call times, okay, times, times, times, this is count. Right? This is the number of times it appears. And this should be count. Okay, so loop through, check number of times does he appear in times plus plus. And then I just set my current count of I is going to equal to the number of times it appeared. And if it does, then yeah, it's good. Now what I'm going to do is I'm going to loop through my queries now. And then I loop through my queries, start from beginning to the end. And then what I'm going to do is I have to loop through again through strings, in order to see if it equals to it, right, if my current string, in order to see if it like, so we've go back to here. Does it appear in your does your queries appear in your strings? Right? So I loop through or AB, right, I have queries, AB, ABC, BC, right, I'm going to check does AB appear in my strings, right? So I'll loop through. Oh, it does appears through it. So if it does appear through it, I just print out the count at the current index, so I'll be too. So that's what I do here. So yeah, here, I have queries loop through the queries, and then I loop through check exists, this should be exists, not inside. It's called exists. So originally, I said exists to false to say that hey, it does not exist yet. But I'm going to loop through the strings and then I check Okay, does this do I find a do I find the same string in my strings array? So if I do, all I do is just I just print out the count at this current query at this current index of I yeah, I just print that I just push back that because that'll be the they'll be the count at it, right, they'll just be the count of where it's at. And then I said my exists equal to true. And then I break. So if exists is equal to false, then I just push back to zero. Okay, so that means we have zero results inside of it, right? There's nothing there does not exist here. So this was basically checking if it exists, if it exists, and I just just return the count at the current index. So queries has, yeah, just return the count at the current index, and they'll tell you it. But yeah, and after that, I just return results. So that's basically the gist of the code is not much to it. So yeah, I hope you guys enjoyed this video. Ray com subscribe. This is how you would do it using just arrays. And if you submit, I got passed all the test cases. Oh, wait, whoa, something, something went up. There's something bad. Oh, this should be J. My bad count should should be counted J. So yeah, you're actually supposed to push back the count at the jth index. So this is like the does it exists? This is going to check if does it exist in a query. If it does, I just push back the count number of times this the string appeared in the array, the jth one. So yeah, hope you guys enjoyed this video. Ray com subscribe. That's all the gist of this video. I'll check you guys later. Peace