How To Solve The Alternating Characters Code Challenge

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

How To Solve The Alternating Characters Code Challenge

Problem

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

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

Covering Our Bases

function alternatingCharacters(s) {
let result = 0;

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

return result;
}

Understanding The Problem

Convert To Array

function alternatingCharacters(s) {
let result = 0;

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

return result;
}

Get Past Reference

function alternatingCharacters(s) {
let result = 0;

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

let past = arr[0];
}

return result;
}

Traverse Array

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;
}

Combine And Compare

// 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;
}
// 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;
}
// 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;
}

Getting The Deletes

// 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

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

// 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

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