How To Solve The Alternating Characters Code Challenge

How To Solve HackerRank’s Alternating Characters Code Challenge With JavaScript

Image for post
Image for post
How To Solve The Alternating Characters Code Challenge

Problem

Let’s break down the challenge into blocks of requirements,:

  • Link to challenge: HackerRank’s Alternating Characters Code Challenge
  • s = string with no spaces in between
  • 1 ≤ s ≤ 100000
  • s will consist of different variations of the letters A and B
  • q = number of queries
  • 1 ≤ q ≤ 10
  • We need to remove sequential duplicates of the same letter

Examples

AAAA → A = 3 deleted
BBBBB → B = 4 deleted
ABABABAB → ABABABAB = 0 deleted
BABABABABABA = 0 deleted
AAABBB → AB = 4 deleted

Goal

Write a function or functions that returns the number sequential duplicates deleted from (s)

Covering Our Bases

To make sure we cover our bases, we no that the default number of number of erased items should be set to zero, and that the length of (s) should be in between 1 and 100000:

function alternatingCharacters(s) {
let result = 0;

if (s.length >= 1 && s.length <= 100000) {
// continue
}

return result;
}

Understanding The Problem

First, the only way we would be able to check every value is to traverse the entire string, character by character, comparing each to its previous. This could be accomplished with a for loop, but we’ll use array filter in this case to traverse things and remove the sequential duplicates that exist.

First we need to convert the string to an array.

function alternatingCharacters(s) {
let result = 0;

if (s.length >= 1 && s.length <= 100000) {
let arr = s.split('');
}

return result;
}

Next we need to keep track of the previous letter in order to compare it with the current letter. We’ll start by getting the first character of the array.

function alternatingCharacters(s) {
let result = 0;

if (s.length >= 1 && s.length <= 100000) {
let arr = s.split('');

let past = arr[0];
}

return result;
}

After we got the first reference we need to go through the array, compare with the past, and then redefine past again, as we continue. Knowing that we can’t compare the first with the first, we’ll need to factor in excluding the first comparison. We also need to remove duplicates from the array, so we’ll use .filter to handle this.

function alternatingCharacters(s) {
let result = 0;

if (s.length >= 1 && s.length <= 100000) {
let arr = s.split('');

let past = arr[0];

arr = arr.filter((item, key) => {
if (item !== past && key !== 0) {
return item;
}
});
}

return result;
}

After we’ve filtered things out, let’s use a test case to see what the results might be.

// Example
// AAABBB
function alternatingCharacters(s) {
let result = 0;

if (s.length >= 1 && s.length <= 100000) {
let arr = s.split(''); // ['A','A','A','B','B','B']

let past = arr[0]; // 'A'

arr = arr.filter((item, key) => {
if (item !== past && key !== 0) {
// 'A' != 'A', 0 !== 0 = false
// 'A' != 'A', 1 !== 0 = false
// 'A' != 'A', 2 !== 0 = false
// 'B' != 'A', 3 !== 0 = true
// 'B' != 'A', 4 !== 0 = true (Something wrong!)
return item;
}
});
// ['B', 'B', 'B']
}

return result;
}

As soon as a new letter is not the same as the last (previous) we need to then redefine the last as that the new letter not the same as the previous (if that makes sense).

// Example
// AAABBB
function alternatingCharacters(s) {
let result = 0;

if (s.length >= 1 && s.length <= 100000) {
let arr = s.split(''); // ['A','A','A','B','B','B']

let past = arr[0]; // 'A'

arr = arr.filter((item, key) => {
if (item !== past && key !== 0) {
// redefine past as the current item
past = item;
// 'A' != 'A', 0 !== 0 = false
// 'A' != 'A', 1 !== 0 = false
// 'A' != 'A', 2 !== 0 = false
// 'B' != 'A', 3 !== 0 = true
// 'B' != 'B', 4 !== 0 = false
// 'B' != 'B', 5!== 0 = false
return item;
}
});
// ['B']
}

return result;
}

Next we need to reattach the first letter from the original string to the beginning of our filtered array and update result with the new value;

// Example
// AAABBB
function alternatingCharacters(s) {
let result = 0;

if (s.length >= 1 && s.length <= 100000) {
let arr = s.split(''); // ['A','A','A','B','B','B']

let past = arr[0]; // 'A'

arr = arr.filter((item, key) => {
if (item !== past && key !== 0) {
// redefine past as the current item
past = item;
// 'A' != 'A', 0 !== 0 = false
// 'A' != 'A', 1 !== 0 = false
// 'A' != 'A', 2 !== 0 = false
// 'B' != 'A', 3 !== 0 = true
// 'B' != 'B', 4 !== 0 = false
// 'B' != 'B', 5!== 0 = false
return item;
}
});
// ['B']
result = [ s.charAt(0), ...arr ];
// ['A', 'B']
}

return result;
}

Lastly we need to calculate the number of deletes we made in comparison with the number of letters that were in the original string. We can do this by just subtracting the total length of the original string by the length of our new array.

// Example
// AAABBB
function alternatingCharacters(s) {
let result = 0;

if (s.length >= 1 && s.length <= 100000) {
let arr = s.split(''); // ['A','A','A','B','B','B']

let past = arr[0]; // 'A'

arr = arr.filter((item, key) => {
if (item !== past && key !== 0) {
// redefine past as the current item
past = item;
// 'A' != 'A', 0 !== 0 = false
// 'A' != 'A', 1 !== 0 = false
// 'A' != 'A', 2 !== 0 = false
// 'B' != 'A', 3 !== 0 = true
// 'B' != 'B', 4 !== 0 = false
// 'B' != 'B', 5!== 0 = false
return item;
}
});
// ['B']
result = s.length - [ s.charAt(0), ...arr ].length;
// 'AAABBB'.length - ['A', 'B'].length
}

return result;
// 2
}

Solution

Putting the full solution together we get the following:

function alternatingCharacters(s) {
let result = 0;

if (s.length >= 1 && s.length <= 100000) {
let arr = s.split('');
let past = arr[0];

arr = arr.filter((item, key) => {
if (item !== past && key !== 0) {
past = item;
return item;
}
});
result = s.length - [ s.charAt(0), ...arr ].length;
}

return result;
}

Test Cases

Now let’s validate the answer with some tests.

// s = "", Expected 0
// s = "A", Expected 0
// s = "B", Expected 0
// s = "AB", Expected 0
// s = "ABB", Expected 1
// s = "BBBBB", Expected 4
// s = "ABABABAB", Expected 0
// s = "BABABA", Expected 0
// s = "AAABBB", Expected 4
alternatingCharacters(""); // 0 ✅
alternatingCharacters("A"); // 0 ✅
alternatingCharacters("B"); // 0 ✅
alternatingCharacters("AB"); // 0 ✅
alternatingCharacters("ABB"); // 1 ✅
alternatingCharacters("BBBBB"); // 4 ✅
alternatingCharacters("ABABABAB"); // 0 ✅
alternatingCharacters("BABABA"); // 0 ✅
alternatingCharacters("AAABBB"); // 4 ✅

Any Feedback

If you have any recommendations on how this can be implemented better or talk about coding, I would love to talk.

If you got value from this, please share it on twitter 🐦 or other social media platforms. Thanks again for reading. 🙏

Please also follow me on twitter: @codingwithmanny and instagram at @codingwithmanny.

Image for post
Image for post

Web Application / Full Stack JavaScript Developer & Aspiring DevOps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store