JavaScript Coding Challenge #5
Today I thought that would be fun to play with another coding challenge from HackerRank. The last one we solved from HackerRank was the Sock Merchant.
Let’s see what is the description of the problem:
Alternating Characters
Shashank likes strings in which consecutive characters are different. For example, he likes
ABABA, while he doesn’t likeABAA. Given a string containing characters and only, he wants to change it into a string he likes. To do this, he is allowed to delete the characters in the string.Your task is to find the minimum number of required deletions.
Strings! I love playing with strings in JavaScript! You’ll see just in a moment why! :D
Note: The input/output format you’ll find on HackerRank is different than what we’re going to have here. For simplicity sake, we’ll build a function nrOfDeletions which will take as it’s parameter one string, and return the total number of deletions Shashank has to perform on that string.
Ok. Now that we sorted that out. Let’s begin! :)
JavaScript solution
First thing we’re going to do is to write the structure of our function. As we said we’re going to take one input: str, representing our string, and return the number of deletions Shashank has to perform in form of an integer, a count in our case, which will be initialized with 0.
function nrOfDeletions(str) {
var count = 0;
return count;
}
As I said above, I love strings, but pretty much all the time I end up converting them into arrays of letters or even arrays of substrings, depending on what is the problem’s description. This is exactly what we’re going to do now using a string helper method called: split. Let’s briefly see what it does:
Considering we’ll have the following string: 'ABABB'. Using split(''), we’ll end up with an array like this: ['A', 'B', 'A', 'B', 'B'], because we used '' as our separator. If we would have used 'A' as our separator we would have the following array: ['', 'B', 'BB'].
You can read more about split on w3schools.
Great! Now let’s use it in our function.
function nrOfDeletions(str) {
var count = 0; str.split(''); return count;
}
You might notice that I didn’t actually stored the array into a variable which is returned when using split and that’s because we don’t need to. This is one reason why I love JavaScript. We can chain multiple methods. You can read more about chaining here.
So… We have a string… then we apply split('') on it and it becomes an array of letters.
All that’s left to do now is to check if two consecutive letters are the same, and if they are, we increase our count. Easy peasy, right? :D
Get Florin Pop’s stories in your inbox
Join Medium for free to get updates from this writer.
Well… we need an array helper which will loop through all the letters and somehow compare the two consecutive ones… This would be very easy to achieve with a for loop, right? Exactly as we did in the Sock Merchant problem.
Right. But then we wouldn’t learn anything new… And we want to make sure we learn something new everyday! This is why we’re going to use the reduce array helper. :D We briefly mentioned it in the Even Fibonacci numbers challenge. If you want to see how reduce works, make sure you’ll check that out.
Ok, but you might ask: Are you sure that reduce is the right array helper to use in our case? The answer is YES, I’m pretty sure, and you’ll see in a moment why. (Note: if you know a better array helper we could use in this case, I’d be very happy to learn more about it, so let me know! :D)
Even though last time we used the accumulator to add up each numbers from the array, we can slightly change how this accumulator works. Instead of storing the data into it for each iteration, we’re going to use it to store only one item, and that’s the previous item in the array.
Might seem confusing, but I’m sure you’ll understand better after you’ll see exactly how does it work in code:
function nrOfDeletions(str) {
var count = 0; str.split('').reduce(function(previous, current) { return current; }, ''); return count;
}
As you can see initially previous is an empty string: ''. But at the end of the function, we’ll set previous to be the current item in the array by returning it. This is exactly what we want. Now we can compare those two each iteration, and if they are equal, we’ll increase our count:
function nrOfDeletions(str) {
var count = 0; str.split('').reduce(function(previous, current) { if(previous === current){
count++;
} return current; }, ''); return count;
}
Notice that first time we’ll check the first item in the array with an empty string. And that’s totally fine, it won’t pass the if statement.
Congratulations! We solved the problem! :D
Conclusion
Today we saw another way to use reduce, and also how split works. It’s extremely useful if you’re going to solve challenges on HackerRank using JavaScript as your programming language. You’ll find yourself in need to manipulate the input they’ll gave you, and that’s where you’ll need split. Give it a try and you’ll understand! :)
As always, I’d love to see your approach for the challenge. Is there a better way to solve it? Write your own solution in the comments section bellow!
Also, if you have other challenges you’d want me to try, let me know! :D
If you liked this challenge, I would sincerely appreciate a click on the Recommend button 💚. Also, you can read about how I solved other Coding Challenges on my profile page. Enjoy! ^_^







