JS - Function that returns the count of characters that have to be removed in order to get a string with no consecutive repeats

Hi,
I’m doing codewars tasks. I have to write function that returns the count of characters that have to be removed in order to get a string with no consecutive repeats.

Examples:

'abbbbc'  => 'abc'    #  answer: 3
'abbcca'  => 'abc'    #  answer: 2
'ab cca'  => 'ab ca'  #  answer: 1

What I have done wrong here?
Below is my code:

function countRepeats(str) {
// return str; // the whole string 
let unique = String.prototype.concat(...new Set(str));
// return unique.length; 
// return str.length; 
let diff = str.length - unique.length; //  should be 6 - 4 = 2
return diff;
}

There are not 4 unique characters in ‘abbcca’
Using unique will not solve your problem.

I recommend that you start more simply, with a for loop that iterates over the string, updating a counter whenever a character is not a repeat of the previous character.

Ok, I got it. Thank you :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.