This course will prepare you for LeetCode style coding challenges. You'll learn 10 important JavaScript algorithms interview questions to help you ace your coding interviews. Mukhtar from Coding Monkey created this course.
He'll help you build a strong foundation in JavaScript algorithms and tackle LeetCode problems with confidence. Hi, welcome back to Coding Money. In about two hours we'll work on top 10 JavaScript algorithms for beginners that will help you prepare for lead code problems. These coding problems are for absolute beginners and these are meant to help you get better at coding and at the same time prepare you for your coding interview at big tech companies like Google where the starting salary is $191,000.
I have a bonus for you at the end of the video. Thank you! make sure you don't miss that.
So let's get started. We'll be reversing a string and also an integer. This is a very common coding interview question one way or the other.
You'll be required to reverse a string or integer as part of your coding interview challenge. So let's get started. All right so we got two coding challenges in this video. We need to reverse the string and then later we'll reverse an integer.
We'll do the string first because once you know how to reverse a string you can easily reverse an integer all you would have to do is convert the integer into the string do the reversal and then convert back to the integer you'll have the solution so we're given a string we need to return a new string with the reverse order of the characters here are some of the examples pause the video right here and give it a try Alright, thank you for trying to solve this problem. If you were not able to come up with a solution, no worries at all. I'll give you one possible solution to this problem. I'll start off by creating a variable called reversed. I'll set it to empty strings.
I'll do something with this variable and then return it at the end. So, what do you think I should be doing to this variable? in order to reverse a string like coding money.
So obviously you would have to loop through each character of the string one by one and add each character to this reversed variable but the next characters that you would insert you would insert before that. So we will be doing something like this one by one to each character. As you can see we'll have something like this at the end.
So to do that we need a loop and I'm sure you're all familiar with the for loop. So we'll do let i equals to 0 i less than str.length i++ and all we have to do is set reversed equal to str.i the current character plus add to the previous ones reversed first and at the end we are just going to return the reverse let me run the code to see if it works so here I'm gonna call the function coding money as you can see it has reversed the string for us so this is a solution but as you can see for this traditional for loop there are many moving pieces like you need to set a variable here you know you need to check for the condition and you need to set the incrementer here so there's uh there you Many areas that things can go wrong so my suggestion would be to use the newer JavaScript syntax you can do the same thing as let car of Str and all you have to do replace this with the car and if I save it and I try to run this again You see it does the same thing, but this is much cleaner. There's even an easier way to solve this problem.
The reason I gave you this solution. because sometimes the interviewer might prevent you from using that built-in function people who have used JavaScript for some time they already know that there's a built-in reverse function why not use that sometimes the interviewer might not allow you to use that so I gave you the solution in case that happens to you so let me copy this solution let me copy the whole function and save it here for your future reference by the way i've created a github repository where i'll be posting a lot more coding challenges i'll post a link in the description so be sure to check that out and follow it because i'll be posting lead code problems and a lot of other coding interview challenges Let me show you the MDN documentation for the built-in reverse method. As you can see, this method is used on arrays.
We can only use this on arrays. However, we are working with strings now. So we need to find a way to convert the string into an array to do the reversal and then convert back the array into a string.
So let's do that. Okay, let's create a variable called str2Array. So the way we convert a string to an array is by using a method called split. So if we don't pass anything, it's going to split each character in the string into an element in an array. So, Once we have that, we can just call the reverse method on str2Array.
We can call the reverse function. This will reverse the array, but we need to return a string. So for that case, str2Array, we need to join it. How do we join it? The same way that...
we use the split we'll just join back the elements in the array into a string so if i run this as you can see we'll get the same output we can even minimize this syntax this code we can write all of this in one line because of something called chaining in javascript so we can change chain the whole thing in one line so how can we do that so let me remove all of it and return so so i'll get the input string i'll call split onto it to convert it into an array then i can call reverse and then i can join it back and this is the reverse string And if I save it, run it, we get the same output. So with this new knowledge that you have of string reversals, you can easily reverse integers. So this should be very easy.
I highly recommend you pause the video right here and give it a try. Thank you for trying to solve this problem. I hope you were able to find a solution.
I'm sure if you did try this problem, you came across the edge case where you have a negative number as the input and you would have to return a negative reversed number. So we're going to take care of that now. There are many ways to solving this problem. I'm going to give you one possible solution. So I'm going to create a variable called reversed.
So you already know how to convert a string. So we need to convert the integer into a string. And to do that, we can use in dot to string method, and then pretty much do the same thing we did for the string, split it into an array.
Now run the reverse on the array. converting it into array because the reverse method only works on the arrays and then we will join it back join if I return this and I let me run this function 15 As you can see, we're reversing it, and we're returning a string. This is not an integer.
We need to return an integer, and also we need to, for example, if I run it with negative 15, you will see that the sign will come after the number. This is not correct. So we need to, first of all, convert the string into an integer. And the way we're going to do that is by using parseInt.
Okay, and and then we need to multiply this with a positive one if if it is a Positive number and if it is a negative number we need to convert this into for example a negative one So we already have a function for that. It's math dot sign and we need to provide the input string in So whatever the sign for this input is, we're going to use that over here. So I'm going to pass the input string over here.
And now if I run the code, you should get the correct negative integer in the reversed order. We are given a string. We need to return true if the string is a palindrome or false if it is not What is a palindrome palindromes are strings that form the same word if it is reversed For example kayak if you reverse this word, you'll get the same word. So we need to return true in that case similarly Madam, if you reverse it, you'll get the same thing.
However for coding money If you reverse it, you'll get something like this, and this is not equal to this. Therefore, we need to return false. So we already know how to reverse a string.
If you do not know how to do that, you can watch the last video. I would like you to solve this problem. Pause the video right here, and then come back.
We will work on one possible solution. All right, I hope you were able to find a solution for this problem. If you didn't, don't worry, we'll work on it right now.
So first of all, we need to find the reverse of the given input string. So we'll create a variable called reversed and we'll reverse the input string str. We'll call the split function on it. Why?
Because we need to convert it into an array. Why? Because we need to run reverse function on it and this function only works on the arrays and once it's reversed we can join it like that.
Turn it back into a string. So now we have under reversed of the input string and we need to check for it now. So if the input string is equal to reversed, then we need to return true. Otherwise, if it fails, then we need to return false at the end. Let's run this function to check, run the function to see if it works.
So I'm going to say palindrome. I'll run it with the kayak first. Let me save it.
And then I'll run. Yeah, so it's working fine. I'm getting.
the desired result which is true let me try with uh coding money we should get false in this case as you can see uh this is working fine and it's a very easy problem and the videos that the future videos will build upon the knowledge from the previous videos so expect that the next videos to get more difficult. So we can write all of this in one line. We can do the comparison right here. We can get rid of this and do the comparison right here like this.
And this will still work. Let me run it. See, it's working fine.
I'm gonna give you an assignment, a homework. You can come up with a solution. There's another technique called the two pointers technique.
If you want to learn about that, do some research, find out what it does and try to solve this problem with the two pointer technique. Or there's another JavaScript built-in method called every. You can use that to solve this.
uh so when you do it just paste your result in the comment section and you'll get a thumbs up from me Today we're going to work on a very common coding interview question. Given a string, return the character that is most commonly used in the string. Example, if you're given a string like A, B, C, C, C, C, C, C, D, then we'll return the character C because C is the most commonly used character in this string. And as you can see in this second example, you see one is repeated the most in this string.
Therefore, we'll return the character one. not the number one. So once you learn how to solve this problem using the technique that I'm going to give you in this video you'll be able to solve variety of other common string questions.
For example what is the most common characters in a string? Does string A have the same characters as string B? Anagram questions. Does the given string have any repeated characters in it? Here's some examples of that.
problems that you would be able to solve by using the method that I'm going to teach you in this video but first I would like you to give this problem a try I'll be right here pause the video alright welcome back I hope you were able to find a solution if not doesn't matter we'll work on it right now just by looking at this problem we know that we need some kind of a data structure to keep the count of each character in a given string. So we would need a character map. Let me give you an example, something like this. A1, B1, and C, not sure how many, like about 7, and D1. Just something like this.
We would need something like this. Okay, so to come up with something like this, we already have a data structure in JavaScript called object. JavaScript objects are like this. We can also use another data structure called maps, which is something new, but we'll use an object in this case.
So But how to do this? How can we, of course, we would have to loop over through each character, like one by one, in a given string, and try to count it, and then store the values in this object. Okay?
So let's get started. Of course, we would need an object. So we'll create an object called character map, car map.
And we'll set it to an empty object. OK? And now we need to loop over the string.
We need to loop over each character in a string and add the value to the character map. So to loop over a string or an array, we can use the same syntax for. for let char of str, str is the input.
So we need to see if there's already a value in the character map. If there is, then we need to increment the value. If, for example, if there's no entry for a we need to create a new entry for a and set it to equal one i mean the first time we set the value we will set it to one and then if we come across the same character we need to increment the value so to do that we will check the character map for that value If we have the character in the character map, then we will increment it by one.
If we don't have it, then we'll... we'll set the value as one okay like for example how how would this uh how this loop would work it's very easy so we're gonna loop over each string and so so for example when we are at a we'll see that if there is a in the character map no there is not so we'll set a equal to 1. Alright, then it goes to B. Do we have B in the character map?
No, we don't have it. So we'll set B equal to 1. So the else part will run. So when it comes to C, do we have C in the character map?
No. So we'll set it to 1. And then again, it will come to C. Do we have C in the character map?
Yes, we have so increment by one so it will become two and it will again come to C See do we have C? Yes, we have so we'll increment by one so it will be three it will keep on doing this and unless until we have the character map with With the count of each character and in our input string so we will have that Right? Just to be sure, let me return it to say that our character map is working. So let me save this.
Let me run this. Okay. We need to call the function. So let me call this one. i need to save it and run it as you can see we have created uh our character map with the count of each character um in the input string so we have something like this as i was showing you in the example before now this this is objects right we need to loop through the object to find which one is the most commonly used Character in the string.
So I'll show you two ways of looping the objects one way is old Way of looping through an object And I'll give you a newer way of looping through an object, which is much easier So let me first of all give you the older syntax and the way that we loop through the object is by converting it into an array and how we can convert an object into an array is by using something called object dot entries and passing the object in our case it is character map So to loop over, then we have, it will return an array with the keys and value. Just like in our character map, these are the keys and these are the values that we created earlier. So we need to use a for loop to loop through this array now. So for that, we say for.
const, destructure it. So I'll destructure and get the key and value from this array. So of object dot entries like this.
And then, yeah, so this is how we're going to. a loop through uh you know an object by convert converting it into a character let me show you how it looks like so let me uh let me console like this so we get we get key and value so let me save this and run it see so we get the key and the value this is uh this is the output from this loop as you can see so we get the keys and values okay all right so now what do we need to do we need to find which one is the maximum like we need to find uh to you know uh give an instruction to the computer to find uh see like the the we can see like we are humans we can see that this one is the most commonly used character but we need to tell the computer to find this one so to do that we need to check each one we need to check each one of these to see which one is the maximum and to keep account for that one we need to create another variable called max and set it to zero and also To get the character, the max character, we'll need to create another variable and set this to an empty string. So now we'll just check for a condition.
Like we'll say if value. Oops. if the value is greater than max, so we'll see like the first one, if is this greater than max, which is zero? Yes. Then we'll set this max to the value and, um, and max jar to the key.
Right? So this will loop through each one. Like this will, first of all, it goes, takes a.
See, is this greater than 0? Yes. Then it updates the value for max and max char to a.
It comes to b. Is this greater than 1? Because it's been changed to 1. yes it is no it is not then it skips that it comes to see it see it sees that whether the value which is 7 greater than 1 yes then set the value to 7 max to 7 and set max char to the key which is what C and return what we need to return the max car let me save this and run it all right so we are getting the correct output from our code this is a working code but now we're going to try to optimize it a bit and i'll show you i said i'll show you a newer way an easier way to loop through the objects all right so we can use similar syntax that we use to loop through the string we can use this syntax to loop through arrays as well so we can use something like this for the objects as well so how can we do that it's just a small little change so we can say let a let key off Car Map. As you can see, this is exactly the same syntax, but we can't use let key off object, which is a car map.
We cannot use that. For the objects, the change that we need to bring is we need to change off with n. So this is a bit opposite of each other.
So when you want to loop through the arrays and strings, you will write off. And O starts like object starts with O. So that's not what you're going to use for objects. You will use N for the objects. So now here to check the value, we need to say char map.
Key. Okay, for the value, this is how we're going to write it. And so for the key, we just write key over here.
Let me save the code, run it again. Okay, we are having a problem here. As you can see, it's case sensitive, so we need to be careful with naming our code.
So let's run it again. See, we're getting the same output. But this one is much nicer, cleaner code. And you just need to remember very few things to be able to loop through the objects.
However, the earlier example had like you need to remember object.entries. And then you need to remember to pass the... object into that method.
So this one is much easier. This is my personal opinion. Also, we can refactor this code to make it much smaller.
So we can convert the same code in just one line of code. So here we can say if character map, okay, and if there is a character map, then we will add one to it or if it does not exist then we'll what we will do is we'll just you know use one so let's see okay let me save it and as you can see This looks much cleaner and short. As you can see, we just convert the FL statement into one line of code.
You can do this, like even if it is too much to do like plus one, you can do something like plus plus character map or one. So if you save it. And you see our code is working fine. Let's run the other test case Should get one.
Yes, it's working just fine. We are given an array in a chunk size Divide the array into many subarrays where each subarray is of length size. For example, if you're given an array with elements of 1, 2, 3, 4, then we need to divide this array into chunks of length 2. For example, the output should be an array containing subarrays of length 2. Here are some other test cases and examples for better understanding of this problem.
I would suggest you to pause the video here give this problem a try. Alright thank you for trying to solve this problem. We're gonna work on one possible way of solving this coding challenge.
The first thing that I do when I get a coding problem is that I use the divide and conquer technique where I get the problem and divide it into smaller problems. So the first problem that I'm looking at is to be able to figure out how to get the first two elements from this array. How to get the chunk of an array, right?
So let me run the function and run this first test case over here. So what I would like to do is to get the first two elements from this array. And the way that we can do that is by using a built-in array function called slice. Let me open up the MDN documentation. It says the slice method returns a shallow copy of a portion of an array into a new array object.
Select it from the start to the end. End not included, where start and end represent the index. items in that array. The original array is not modified. So let's use this to get the first two elements.
So the way that we can do that is array.slice. The arrays are zero indexed, which means the first element is zero, then it is one, two. and three like this.
So to be able to get the first two, we'll provide the first index which is zero. And then the second index is two. So in this case, we have this two. So this is great. We can use the size here.
So So let me return this and see what we get in the output. Yeah, so we get the first two elements from the array. So now we solved the small problem that will come in handy in solving the bigger problem, right? So now we know that we need to return an array, right?
So we would need to create an... empty array here let's call it result so and at the end we will return this result okay we will return this result but first we need to get the chunks from array and push it in this array right so so How we can do that? We can do it like this result.push and we can put we can push this chunk inside of this array, the empty array that we have created. However, this will only push. the first two elements.
Let me save this. And if I run it, you will see we only get the first chunk. However, we're not getting these other two.
So we need to repeat this and we need to a variable to keep track of the index, right? So let's use a variable called index and set it to 0. So instead of the zero, we will use this variable. Okay. And we need to repeat this code, this line of code for all the elements of the array.
As long as there are elements in the array, we need to keep on repeating this. So for that, we will use a loop. So when you want to repeat.
a statement or code, that's when you would use loop. So I'll use a while loop while index is less than the array that linked as long as there are elements in our array, we want to keep on executing this line of code. right but this will run forever this will be an infinite loop because we're not doing anything to make this stop right and this will keep on running forever we will we need to increment the index we need to increment the index we need to make sure that when the index is greater than the array dot length then it should exit the loop so how can we do that we will increment the index with the size right and so this will start from 0 to and the next time I would like to add index with the size so that the next time it will get from two it will start from two two, four, right here. But since in this example, we don't have the fifth element, which is going to be index of four, we don't have that. So it will not this function will not take the slice function will not take the end as we read in the documentation.
So in that case, we will have three and four. so let's save this and run the code as you can see now we're getting the correct output so let me try to run with this example and see if it is going to work for this yeah so we're getting the correct result and That's the solution for this coding problem. That's only one possible solution.
There are many ways of solving this problem. If your solution is different than this, then it's not a problem as long as you're getting the desired output. We need to write a function that accepts a string.
The function should capitalize the first letter of each word in the string, then return the capitalized string. Here are some examples. I want you to open the 5-titlecase.js exercise file and give this problem a try.
Pause the video right here. Thank you for trying to solve this problem. Every time I get a coding problem, what I do is use the divide and conquer technique to divide the problem into very small problems so that it's easier to find a solution. So here the first thing that I think we need to figure out is to change the case for the string to make it an uppercase. So let me return str to upper.
This is the function that we will call on the string to change the case. So now you can see it's all capital letters, right? However, we would need only the first letter of each word. to be a capital, not the whole sentence, right?
So what we need to do is to, to break this string, to split this string into an array with each word. And how can we do that? So let me say, const, let me create a variable words, and I will call split function on the string and I'll provide a space so what I will do is I will split this string based on the spaces so this will become like something like this Now that we have all the words, what we can do is create another variable called result, right?
And set that into an empty array. And we will capitalize each word and push it into this array. And at the end, we will just return this result, okay? And...
To do that, we need to loop over the words. So we need to loop over the string right with each word. So we can use a for loop for let word of words. So now we have access to each word.
And we want to push that result dot push, we want to push that word into this result. array. And so what do we want to do, we want to get the first letter of the word. And how can we do that, like this. So for each word, like the first time it loops over, you'll get this word.
So you'll get this word. So the first letter that you can get it by, you know, like doing like this, okay. So this is the first word.
But we need the rest of the characters. to be added to this word. So how can we do that?
So we need to add that. If you watched the last video you know how to use the slice function. So what we will do is we will call slice on the word and we'll start from the first position. Right?
As you know that strings and arrays are zero index so So this would be 0123. So we got the first letter. However, we want to add the rest of the letters. So we'll start from the position one.
So it will take all of this and add it here, right? So once we capitalise each word, you will go the loop will go through each word each word like this one by one and capitalize the first letter and add the rest of the characters to the word and so we will have all the words in the array so let me save this and run the code you will see that okay so it's not been capitalized because we haven't call the to uppercase function onto the first letter. So let's do that and run the code again. Now as you can see each word in this array is capitalized but this is inside of an array.
So we need to join the elements in the array into the string based on the space right so i'll save this and run this again this is the correct output and this is how we capitalize the sentence and if you don't like creating a new array and adding it adding each word into that array and returning the result we can even minimize all of this work by using map method. So we can call map on this words. So we can get rid of all of this, we can call the map function, and we'll get access to each word, right. And the same way we will get access to the word we'll get the first letter we'll change this to uppercase we'll add the rest of whoops called the word we'll call slice on on the word and add the rest of the characters of the word and then we'll join with the space and return the result let's run it see this is another solution so you got two solution for this problem we no longer need this variable so we can remove this check to see if two provided strings are anagrams of each other One string is an anagram of another if it uses the same characters in the same quantity.
Only consider characters, not spaces or punctuation. Consider capital letters to be the same as lowercase. Here are some examples.
If you provided two strings, one coding money and the other money coding, it should return true because it uses the same characters and the quantity of each character is the same therefore it returns true. Same is true for this other example. rail safety and fairy tales or anagrams of each other because they use the same characters and the same quantity of characters are present in both of these strings.
We will not consider exclamation marks or special characters or spaces. So this is the challenge for today. Pause the video and give this problem a try.
Also if you want to follow along check out the github repository at this URL. Alright, so I'll give you two possible solutions to this coding problem. The first solution consists of three steps. In the first step, we'll build a character map for string A, then we'll build a character map for string B, and the third step we'll compare both of these character maps to see if they're equal to each other.
If they're not, we'll return false. So let's start by building the character map for string A. To do that we need an empty object. let's create that let's call this car map a and set that to an empty object and also we need to remove space in punctuations exclamation marks and things like that so i'll do that now string a dot to lower case and remove the the punctuations and spaces we would we'll use the regular expression make sure to use backslashed capital w this will match all the special characters the space and punctuations this is a character set the brackets represent the character set and this capital D represent anything that is not a word which is different than the small W if you use a small W it will match all the words that is in a character it will not match the punctuations so we'll use capital D to replace that. Okay.
So, uh, to show you, um, uh, let's return, uh, this string a, let me return it, uh, save this to see the output of, um, our clue clean, uh, strength. So as you can see, um, this is the strength, eh, it has space and exclamation marks. but after this line of code it removed exclamation mark in spaces and this is the output that we get all right so let's create now the character map so we'll use a for loop so for let car of string a and so we'll say car a map a dot let's see car map a if this character already exists if it exists then increment it by one if or if the if it does not exist then i use one Okay, so this will create the character map for string a let me return it to see the output of what we get here So return car a save and run So you see we're getting the character map for string a So as you can see it's counting each characters.
For example, we have a 1 or 2a, 1i, 1l, so on. So we created the character map for string a. We need to do the same thing, the same exactly same steps.
There's a principle in software development, do not repeat yourself. So if I write the same code over here, it's repeating myself, writing the same code again and again. So At this point, it is a good idea to create a helper method, another function to create the character map. So I'll call this new function, function, I'll call it character car map, call it car map.
And it will have STR. And I'll just... what i'll do copy this code i'll paste it here right so turn car map so uh let's do something remove these things so that we can reuse it with anything so i'm just going to change this to str because this is the variable that we're using here str and then STR.
Okay, remove the A from here. And so I'm going to save this. What I'm going to do is here for character map A, I'm going to use car map provide string A to this one, string A. And let me return this map A to to make sure it's working fine.
So yeah, so this is working fine. And now the best part is that we can reuse this to build a character map for string B. So I can copy the statement and paste it right here. Rename the variable, call it B.
And here, I'm just going to change string A to string B, because this is what we're using as the parameter and now so we we have uh the character map we build the character map for both the string a and string b now we need to compare compare both of them first of all let's compare the length of both the characters so to do that as you can see in an object we have keys and values as you can see r is the key and one is the value a is the key two is the value right to get the keys to get all the keys we can use object dot keys and we can provide the string like that so let me return this return object.keys so that you see what would be the output Now, so if I return this object that keys, oh, sorry, I have to provide the carry character map, car map a, so let me save it. So So you see, these are the keys, it returns all the keys for me from the object, the character map of string a. So what I can do is I can compare both of them by using an F statement.
I can say object dot keys provide car map a and to find the length because this object keys convert into an array and then you get the keys in an array so on the array we can use linked to to check the length and compare that with object dot keys, car map B dot linked. So if the length of these two keys do not match, then we'll return false, right? So if our character map a does not have the same keys as the character map B then obviously we know that this is going to be false this is not going to be an anagram now we need to loop both of these character maps so we can loop over any of them we can either choose A or B it doesn't matter so I'll use character map A let's say let key n car map a now you see that we're using n you see let key n when we're working with objects and we want to loop through the objects we use n and if it is an array then in that case we'll use off so now we're using let key in character map a and then i'll use comparison i'll do If car map a key is not equal to car map b key, if the key in character map A is not equal to the key of the character map B, for example, if we have, for example, one are in character map A, and we have two are in the character map B, and if they're not equal to each other, then in that case, we know that this is not an anagram, therefore we will return false, right? So if this chick passes and this chick passes and we can't find anything that is not equal to each other then it means that it is an anagram and both of the characters and character map a and B are equal so this means that we have an anagram so in that case we will return true so Let's save this and let's run the code to see if it is working or not.
As you can see this is not working. Why? This should return true. Why it is not working?
So it is returning false. We have to find what is the reason. Oh, here, this is a big mistake that I'm doing. I am checking if they're equal. I should check if they're not equal.
So this should be like this. They should not be equal to this. In that case, we would return false. All right, so let's save this and run the code. It should give you the correct output this time.
Let's try with another example. So let me try with this. All right, let me save it. This should return true.
The last test case, let's try with this one. Copy. paste save it okay and now it should return false this is working this is the first solution to the problem let's work on the the easier and very intuitive solution to this problem all right let's work on the second solution i said i'll give you two solutions and the second solution is is much easier i'll show you one small little trick it will make it a piece of cake So let me copy this one for your future reference.
I'll copy the solution as well for you. And I'll copy it, paste it here. I'll comment it out.
Okay. And now let me get rid of this. So now I'll just...
get rid of all of it right so now what we will do is we will sort a string a in string b once it's sorted then we'll get the it will make it much easier right so let's do that so let's sort string a string a but you know that the fourth function is only possible on arrays it cannot be done on the string directly so we need to convert the string into an array but before doing that we need to change this to lowercase lowercase and also we'll do the regular expression the same regular expression that we did to get rid of the and punctuations. So once we got rid of that I will call split function split function on it and then once I have changed this to an array I can call the sort method and once it is sorted I will Join it back like this. So let me return this to see what we get.
Let me save this. Return. As you can see, this is high there, right? So this is string A. So we can do the same thing for string B.
So let's create another function to do this. to make it easier because we know about the software development principle do not repeat yourself we do we will not repeat ourselves therefore we'll create a function uh i will call this function clean str and i'll provide this string as the parameter so what i'll do is i'll just cut this from here i'll paste it here return, sorry, return, instead of stress strength, we need to use the same input parameter name, the same thing like this. Okay, so now we can do the same, like we can return clean str provide a string a and compare this to clean str string B so now when we do this we'll just compare this well this is a conditional statement if it is true it will return true if it is false it will return false so let's also try with another example this one copy paste save run it we are getting the correct output this is the second solution as I promised to give you we need to write a function that returns a number of vowels used in a string what's a vowel vowels are the characters a e i o u here are some examples so we need to count the vowels i e e so there are three vowels we need to return three and second example how are you we have five vowels so we need to count the vowels uh in a given string uh pause the video give this problem a try All right, so I'll give you two solutions to this problem.
I'll give you one solution with the regular expressions. That is the easiest. And a lot of interviewers might not like you to use the regular expressions.
They may not allow you to do that. So I'm going to give you another solution just in case. So with the regular expression, it's very easy.
So we have a built-in function called match. which we can call on string which is the provider string so we can call match and then we will provide a regular expression so what regular expression we will provide here will use a character set of a e I owe you right and then we need to provide two flags G and I why we're providing G because by default if you do not have G over here it will stop at the first match that it finds. For example, if it finds A, so it will only return the first match.
When we have G, it will not stop at the first match. It will go forward and it will find any character that matches the letters in this character set. And the I is for case insensitivity. So it will become insensitive to the case, even if it is... a capital letter, it will still work.
So, so, um, this one, the match, uh, method returns, um, null. If, uh, there are no matches, if, uh, there are any match, it will return an array with all the matches. So, uh, therefore what we will do is create a, uh, variable called matches and, uh, and store the result of the matches inside this matches variable and we'll return matches if there are matches we will return the length we don't want to return the match because as you can see in the examples we just need to return the count of those vowels so we'll just use a link because on arrays we can run we have a property called length and it will return the length of that array.
And if there are no matches, we can return zero. So in that case, I'll save this and I run it. As you can see, for coding money, the in the example we have this is returning the correct result.
So this was the first solution with the regular expression. All right, let's work on the second solution to this problem. So the idea is we'll create an array.
with all the vowels inside it so then we will check the strength against the values in the array to see if we can find a match if there is a match we will use the counter variable to increment and then at the end we'll return that counter variable so let's try to do that so let's create an array called vowel check and so what I'm gonna do let's try to copy from here so I'm just going to copy the vowels in this array so now we have all the vowels right sorry I try to use a camel case okay so now I also need a variable called account and I'm going to set that to zero and then I'm going to run a loop for let car of STR okay I'm going to have a logic inside of this loop and at the end I'll return the count what i will do it inside of this loop i will check to see if the characters in the string is a vowel if it is a vowel i'm going to increment the count variable if it is not a vowel i'll not do anything and so at the end we'll just return the count okay so the loop goes uh each character one by one like this right so in this example we have coding money it um the first iteration it will check for C then O D I N G. So I'll say so the arrays have a built in helper function called includes that's also available on this string. But in our case, it's better to use an array, it's more structured and organized. So we can say if vowel check dot includes okay if the vowel check includes the character from the string so if it includes character so in the first iteration see if is see inside of this array if it is if it is not then it will just skip this line if and the second iteration it will come to oh so it will check to see if always inside of the vowel check if it is then we will increment the count variable so at the end we will return the count so let me run it in the example we should get the four so let's see As you can see we're getting the correct result. This is another solution to this problem. One more thing before you go you can just add two lower keys over here to make sure even if the provided string has capital letters or small letters it will still match.
Our JavaScript training algorithms for beginners would not be complete without the classic fizzbuzz challenge. I need to write a program that console logs the numbers from 1 to n, but for the multiples of 3 print fizz instead of the number, and for the multiples of 5 print buzz, for the numbers which are multiples of both 3 and 5 print fizzbuzz. Here's an example. I would like you to try this problem and pause the video right here.
thank you for trying to solve this problem i hope you were able to find a solution if you did not this doesn't matter we'll work on it together the whole trick to solving this problem is using the modulo operator if you haven't used that before we'll use it right away okay of course we need a loop right so we'll start from one i let let i equals to one because it's given in the directions and the condition that we need to satisfy is i less than or equal to n and then i++ okay so the first thing that you would need to do is to check for if the number is a multiple of five and three if it is so how to check if a number is a multiple of three we use the module operator so we can say i the module operator is the person sign If i is, if i modulo 3 equals to... zero it means that if the if I is divisible by three completely and it does not have any remainders then it means this number is a multiple of three okay and so I also need to check if I is a multiple of five if if I is divisible by five and it does not have any remainders then in that case we need to write phase bus right else if if i is multiple of three and if i is divisible by three and it does not have any remainders after the division and then it it means that this is also we use the modulo operator every time if we need to do like for example every third time or every second time in a repetition so that's how like i'll show you uh now in the example okay so in that case if it is multiple of three we need to print fizz so console.log fizz else if I is a multiple of five. We need to write buzz console dot log else console dot log the number I. Okay. Let me save this and run the code.
as you can see i provided the number five here to the function and as you can see that i have the number one two and then it says phase and then i have four and then it says buzz okay let me try with the bigger number like 20 okay let me save it and run the code as you can see so like i said if a number is a multiple of three then it means then It means that it will repeat every third time. You see, this is the third, you know, every third time we have phase. Every fifth time we have buzz.
And every time that the number is divisible by three and five, and there is no remainder to that number, like, for example, 15, we write phase buzz. Okay. That's the solution to the classic fizzbuzz challenge. Next, we need to write a function that accepts a positive number n.
The function should console.log a step shape with n levels using the pound character. Make sure the step has spaces on the right hand side. Here are some examples.
The function accepts n number and if we are provided with number 2, this should be the output. Okay, so give this problem a try and pause the video right here. Alright, thank you for trying to solve this problem.
If you were able to find a solution, that's great. If not, it doesn't matter. We will work on this problem together.
I think first we need to focus on printing something like this. It would be easier for us to print something like this and then we will worry about the spaces later. Let's do this one and then we'll take it from there.
as you might have already guessed we need a loop so I'll write the for loop i equals to 1 i less than or equal to n i plus plus okay so one thing I've noticed that beginners are confused about whether they should start from 1 or 0 if they want to start from 0 then I'll make sure that the condition is i less than n but if they want to start from one then the condition has to be i less than or equal to n so that's important to note uh for now i'm just going to console like the pound sign i'm going to save it and run it as you can see we get the three pounds but we need nine pound symbols, three on each line. Okay. So to do that, we would need to use a nested loop. I noticed that the beginner developers find working with the nested loops difficult. They struggle with it.
I'm going to give you a way to make it easier. If you visualize what you want to do in terms of a table containing rows and columns, it makes it a lot easier. So, Let's try to do that. I'm going to rename my variable. I'll change it to row and then I will create a nested loop and I'll make this a column.
So the nested loop, the outer loop will be responsible for the row and the inner loop will be responsible for creating all the columns in that row. So if you think about it this way then it would be much easier to work with the nested loops from now on. Okay so let's console.log here just to show you what you get as the output. As you can see, we're getting 8 pound symbols in the output.
But we would we need only 3 pounds on each line. So it's going to be a 3 by 3 pound symbols. And we need that in the output. Okay, so how to do that? Okay, so I want to create all the columns.
I mean, I want to create an empty variable called line, I will set it to empty string. And what I'm going to do, I'm going to append, append the pound symbol to this line. So after it completes, it adds the pound symbol for all of the columns for that row, I want to console log that line okay let me save this and run as you can see we're getting the desired output right alright so the best way to visualize the code that I've written here so far by debugging it so let me run the debugger i have set a breakpoint on the first loop let me uh let me watch for some variables let me watch for row and call and line okay and let's run this okay so the outer loop starts at one and then we initialize the variable line to empty string and then it executes the inner loop it starts the inner loop at one column one and then it appends the the pound symbol to the line variable then it will increment the column to two and then it will append another pound as you can see and then it increments to column three and since we are provided with a number n you see n is three so now we know that the inner loop will break and so the row will increment now we're working on the second row and then again we initialize the line to an empty string and then the inner loop will run three times to append three pound to the line and then we'll console log the second line now we're working with the third line in the loop and initialize the line and then we're appending the pound sign to the line and then once row is equal to four the condition becomes false so it you know exits the loop so this is the code when you have trouble understanding the code it's always a good idea to debug it okay so now we have we know what this is doing Now we need to take care of the spaces on the right side. We need to do what let's let me illustrate that what we need to do here. So we need to remove that replace the for the first line.
We need to have two spaces on the second line. We need to have one space. Now we need to work on getting an output like this.
It will be easier for me to explain this on the blackboard. So let's get on the blackboard. Oh, so whenever we're working with the nested loops, it's always best to visualize it in terms of a table in rows and columns. It makes it much easier to work with the problem. So let's see on which columns do we have the pound symbol, right?
We need to figure that out. We have it here column 1 row 1. We have it column 1 row 2. As you can see this is 1 is equal to 1. Column 1 is less than 2. Let's see here. Column 2, row 2. We know that 2 is equal to 2, right?
And let's see this one. There's column 3, row 3. We can see that 3 is equal to 3. Just by looking at this, we know that all the columns that have the pound symbol satisfy this condition. Column is either less or equal to the row.
Alright, since we figured out the condition logic, the code is easy. So here we'll say if. column is less than or equal to row then append this line append pound to the line else append space let's save it and run the code As you can see we're getting the desired output.
This is the solution to the problem. Let's try with another number like let's try with six oops as you can see we're getting the correct string pattern. We need to write a function that accepts a positive number n.
The function should console.log a pyramid shape with n levels using the pound character. Make sure the pyramid has spaces on both the left and the right hand side. Here are some of the examples as you can see that it has spaces on the right and left hand side of the pound symbol whenever there's needed.
Okay so I would like you to give this problem a try. Pause the video right here. As you might have probably guessed we need a loop right we as you can see if the number that's provided is three we need three rows right so we need to write a loop like I mentioned in the video before this one that whenever you're working with nested loops it's best to visualize them in the form of a table consisting of rows and columns it makes the problem so much easy so let me write the for loop let row equals to zero row less than n row plus plus that's easy right so we need three rows and for each line you see this is one line I'm calling each row a line, right? So I'm going to need an empty string. So I'm going to create a variable called line and I'm going to set it to an empty string.
Now we need to loop over the columns, let the column equal to zero, and column less than, okay, so we need to figure out the number of columns, because it's uh as you can see for if the number is three we need five columns so uh let's hop on to the blackboard real quick okay so what do we need to figure out is the number of columns because number of rows is clear for if n is equal to two we have two rows if n is equal to three we have three rows right but the number of columns if it is 2n minus 1. Does this satisfy the number of columns? So if the number is 3, 2 multiplied by 3 is 6, minus 1 is equal to 5. So this satisfies the number of columns. So now we can easily write our inner loop. And we can write it like, let me select the same color, 4let. column equals to zero column is less than two n minus one column plus plus right so now we need to figure out which columns do we need to put the pound symbol in right so if you look at it we know that the middle column always has the pound symbol.
So let's figure out the formula to find the middle column. So if we have five columns and we divide it divide that by two we get 2.5 and if we round down this number we get two right and you can see that the two column has all of it has the pound symbol okay we have a method called math dot floor and to get the number of columns we already know that it is 2n minus 1 and we divide that by 2 we get the midpoint we figured out the midpoint now we know that there's a few of the pound sign on the right side of the midpoint and there are a few of the pound sign on the left side of the midpoint. So if you see this carefully and you observe it you will figure out that all the columns that have the pound symbol satisfy this condition and the condition is column is either greater than or equal to mid minus row and column is less than or equal to mid plus row. Okay, so now that we have figured that out, it is so much easy to write the code. So let's hop on the Visual Studio Code and write the code for this one.
All right, so as I've explained to you on the blackboard, we now know the number of columns. That is 2n minus 1 and then we get column++. All right. And we also know the condition to use for you know determining which column should have the pound symbol and that is if right if column is greater than or equal to mid midpoint minus row and column is less than midpoint uh less than or equal to midpoint plus row all right so we need to uh we don't have a variable mid uh we need to create that here i'm gonna use the mat.floor and 2n minus 1 divided by 2. Right? Okay.
And now if this condition is satisfied, we know that we need to append the pound symbol. If not, we need to append space. Okay, and right after this inner loop ends, we need to console log the line. Let me save this and run it.
Cannot mix big int and other types, use explicit conversion. So I'm getting an error. Let's see what error is this. I know what the problem is.
We need to multiply 2 multiplied by n minus 1. And that's the same thing we need to do over here. We need to multiply this like this 2 multiplied by n minus 1, right? So let me save this and run this again.
As you can see I'm getting the correct output this time and let me try with another let's try with nine let me save it and run it as you can see we're getting a bigger parameter this time and this code is working perfectly. I've had dreams that weren't just dreams. Today we need to write a function that accepts an integer and it returns that.
n by n spiral matrix. Here are some examples. If we are provided with an input number 3, we need to return a two-dimensional array with the values in the same order as 1, 2, 3, 4, 5, 6, 7, 8, 9. I'd like you to give this problem a try and pause the video right here. This is not an easy problem. I would say this is a medium to a hard problem.
If you were not able to find a solution to this problem, that is very normal. I was struggling with this problem the first time I tried it. But once you get the trick, problems like this become so much easier.
Okay, so as you can see, we need to define a few variables like start row, end row, start column, end column. and we would need a counter variable and also we would need to take care of the four sides the top row we would need one loop for the top row another loop for the right column another loop for the bottom row and another loop for the left column once we so we we would need at least four loops and once we take care of the outer uh you know the four sides outer sides we would need to put the four loops inside a while loop to take care of all the inner four sides of the matrix right so our first loop would go from start column to end column and this would take care of the top row right and once we do this we would increment the start row by one so it will move down here and then we would need another loop that would go from start row to end row and that would take care of the right side at the right column right and Once we do this, we decrement the end column. So we decrement the end column.
It will come over here. And then we need another loop that would go from end column to start column. And it would take care of the bottom row. And once the bottom row is complete. we would decrement the end row so it will come over here and then we would finally run a loop from end row to start row that would take care of the left side the left column of the matrix And then once we write these four loops, we need to put them inside the while loop.
And the while loop would take care of the inner four sides. So once you know this, the coding becomes so much easier. So let's start coding. I'll start off by creating a variable called result. I'll set that to empty array.
And we need to return a multi dimensional array. So to create that we need to run a for loop for I equals to zero I less than n, I plus plus result dot push empty array. return result let me run this code to say yes we're getting four empty arrays inside of this array because the number that is provided as the input is four okay so we need to have four rows in the matrix we but these are empty we need to fill them with values and once we're done at the end we will return the result okay and so we need to take care of the four sides top right bottom left top right bottom left okay to create this top row we need to run a loop from but first we need to create some variables the variables that we need to define our counter I'll set that to 1 star row I'll set that to 0 and row n minus 1 start call equals to 0 and call is n minus 1 okay so for the top row we need to start for let i equals to start call i less than or equal to call I plus plus make sure you have semicolons here and result okay so the first value that you'll provide will be for the row and the second values are going to be for the column okay since the the values for the columns are going to be dynamic and it will be coming from I but we know that the top row is start row so I'll do start row equal to counter and then I will increment the counter like this let me save this and run it Okay, as you can see, the top row is done.
So we need to increment the star row. Okay, like that comes here. Okay, let's do this the same thing for the right side.
This time it will be star row because it's going to be vertical. comes from the top to the bottom row so it will be start row end row i plus plus this time the value for the row will be dynamic but we know that the column that needs the values changed are going to be the end column we're going to be adding the values to the end column. So and at the end, once this end column is done, we will decrement the end column. So it's going to be end call minus minus, I'll save it, I'll run it. As you can see, the top row and the right column is done.
Let's take care of the bottom row. So for the bottom row, let me copy this one. So for the bottom row, as you know that we will start from the end row, and column and is greater than or equal to greater than or equal to the start column and we need to decrement the value of I the column is going to be dynamic but we know that since it's going to be the bottom row so it is going to be the end row and once that is done we will decrement the end row like that okay let me run this to see it's working fine so the top right side in the bottom row is done so we need to take care of the left side so for this one for this one we know that it's going to go from end row End row, it's start row.
We know that the values for the rows is going to be dynamic but the values for the columns is going to be start column and once that is done we will increment the start column like this. So let's run this one. Okay, so the four sides are done. 123456789 1011 12. Right. So we need to put these four loops inside of a while loop to take care of all the inner four sides of the matrix.
So let me copy the four loops that we wrote over here, I'm going to cut it and here I'm going to write a while loop while start row is less than or equal to and row and start column is less than or equal to end column While this condition is true, you know, execute this code within the body of the while loop, which are the four loops, it will keep on looping until it reaches the end of the row and end of the column. So let me save this and run the code. As you can see, we are getting the desired output.
and it's going into the right direction one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen Let's try with another number like 6 and run the code. We are getting the bigger matrix with the correct values. Now you're ready to start my leetcode video series.
You should find links somewhere on the screen to the playlist.