This is my unique code snippet
function countTriplets(arr){
let sequence = (arr.join(''));
let pointer = -1;
let tripletCount = 0;
const findTriplet = (char, start) => {
return sequence.indexOf(char, start + 1);
}
const updateCount = () => {
tripletCount++;
pointer += 3;
}
while((pointer = findTriplet('hhh', pointer)) !== -1 ){
updateCount();
}
while((pointer = findTriplet('ttt', pointer)) !== -1 ){
updateCount();
}
console.log(tripletCount);
}
countTriplets(["h", "h", "h", "t", "h", "h", "t", "t", "t", "h", "t", "h", "h", "h", "h"]);
In this implementation, I aim to determine the number of times 't' or 'h' occur consecutively three times in a row.
I am exploring if there exists an efficient way to accomplish this without converting the array into a string or merging the two while loops.