Public
Edited
Oct 13, 2023
2 forks
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
async function* stateGenerator(inputString, tick = 500) {
const allStates = generateAllStates(inputString);

for (let i = 0; i < allStates.length; i++) {
yield allStates[i];
await Promises.delay(tick);
}
yield {};
}
Insert cell
function generateAllStates(inputString) {
const states = [];
const state = {
start: 0,
end: 0,
window: new Map(),
max_length: 0
};

states.push({ ...state, window: new Map(state.window) });

while (state.end < inputString.length) {
const { window, max_length, start, end } = state;

const rightChar = inputString[end];

// need to shrink if invalid
if (!window.has(rightChar) && window.size == 2) {
const leftChar = inputString[start];
while (window.has(leftChar)) {
window.set(leftChar, window.get(leftChar) - 1);
if (window.get(leftChar) == 0) {
window.delete(leftChar);
}
state.start += 1;
states.push({ ...state, window: new Map(state.window) });
}
}

if (!window.has(rightChar)) {
window.set(rightChar, 0);
}
window.set(rightChar, window.get(rightChar) + 1);

// animation works because we don't emit state change event for end until

state.end += 1;
state.max_length = Math.max(state.max_length, state.end - state.start);
states.push({ ...state, window: new Map(state.window) });
}
return states;
}
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