JavaScript Coding Challenge #10
Ever heard of CodeFights? If you didn’t, you must sign up now! It’s a lot of fun, I promise! :D
I’ve played some time and last week and I ended up on top 1. ^_^
CodeFights is a website where you can test your coding skills against friends, co-workers and company bots. And the best part is that they have support for a lot of programming languages, including JavaScript. Yey!
It’s great if you want to prepare yourself for an interview because they have a lot of interview problems.
Today we’re going to take a look at one of them which is called: the Line Encoding problem:
Given a string, return its encoding defined as follows:
First, the string is divided into the least possible number of disjoint substrings consisting of identical characters, for example,
"aabbbc"
is divided into["aa", "bbb", "c"]
.Next, each substring with length greater than one is replaced with a concatenation of its length and the repeating character, for example, substring
"bbb"
is replaced by"3b"
.Finally, all the new strings are concatenated together in the same order and a new string is returned.
Example
For
s = "aabbbc"
, the output should belineEncoding(s) = "2a3bc"
.
To be honest, it took me a while until I figured it out, but it was a lot of fun! I recommend you do the same before you continue to read this. It will help you a lot on the long run if you struggle with it instead of just jumping to the end.
My approach is a bit different then what they suggest in the problem, but it does the trick. :D
First we need to loop through all the letters of the string and compare each pair of two consecutive letters:
function lineEncoding(s) {
for(var i=0; i < s.length; i++){
if(s[i] === s[i+1]){ }
}
}
We’re going to add a count x
which will hold the number of identical consecutive letters, and also a variable result
in which we’re going to save the final output of your program:
function lineEncoding(s) {
var x = 1;
var result = ''; for(var i=0; i < s.length; i++){
if(s[i] === s[i+1]){ }
}
return result;
}
We initialize x
with 1
and increment it by 1
every time the next letter is identical.
function lineEncoding(s) {
var x = 1;
var result = ''; for(var i=0; i < s.length; i++){
if(s[i] === s[i+1]){
x += 1;
}
}
return result;
}
Now we’re going to store a concatenation of x
with the current letter s[i]
everytime the next letter s[i+1]
is different. We also need to reset x
:
function lineEncoding(s) {
var x = 1;
var result = ''; for(var i=0; i < s.length; i++){
if(s[i] === s[i+1]){
x += 1;
} else {
result += x+s[i];
x = 1;
}
}
return result;
}
Great! Now let’s test it:
lineEncoding('aabbbc'); // 2a3b1c
lineEncoding('aabbbcccc'); // 2a3b4c
Hmmm… It works almost well… but the problem is that we print 1
also. The problem says that we can only print the length of the substring which is bigger than one.
This little issue can be solved easily with the string’s replace function:
function lineEncoding(s) {
var x = 1;
var result = ''; for(var i=0; i < s.length; i++){
if(s[i] === s[i+1]){
x += 1;
} else {
result += x+s[i];
x = 1;
}
}
return result.replace(/1/g, '');
}
We’re passing a regular expression /1/g
which matches every occurrence of the character 1
and replace it with an empty string ''
.
Conclusion
Do you have a better solution for this problem? Please don’t hesitate to let me know about it in the comments sections bellow. I’ve learned a lot from people who submitted their own solutions on the previous coding challenges. Find out more about them here.
I hope you enjoyed this and I would sincerely appreciate a click on the Recommend button 💚.
Happy coding! ^_^