Public
Edited
Dec 6, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
testInput = "mjqjpqmgbljsphdztnvjfqwrcgsmlb";
Insert cell
Insert cell
function findFirstUnqiueNChars(buffer, n) {
// This is not efficient, but it works
for (let i = n; i < buffer.length; i++) {
const start = i - n;
const substring = buffer.slice(start, i);
// Are there any repeats in the substring?
const charCount = new Map();
let foundDuplicate = false;
for (let j = 0; j < substring.length; j++) {
if (charCount.get(substring[j])) foundDuplicate = true;
else charCount.set(substring[j], true);
}

if (!foundDuplicate) return i;
}

return -1;
}
Insert cell
findFirstUnqiueNChars(input, 4);
Insert cell
Insert cell
findFirstUnqiueNChars(input, 14)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more