Skip to content

Instantly share code, notes, and snippets.

@ycmjason
Created June 7, 2018 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ycmjason/e3af4f23cd7a670f6f3b1178688f9e67 to your computer and use it in GitHub Desktop.
Save ycmjason/e3af4f23cd7a670f6f3b1178688f9e67 to your computer and use it in GitHub Desktop.
const crushOnce = (xs) => {
if (xs.length < 3) return xs;
let count = 0;
for (const x of xs) {
if (x !== xs[0]) break;
count++;
}
if (count >= 3) {
return crush(xs.slice(count));
}
return xs.slice(0, count) + crush(xs.slice(count));
};
const crush = (ss) => {
const crushed = crushOnce(ss);
if(crushed == ss) return ss;
return crushOnce(crushed);
}
crush('aabbccddeeedcbak'); // "k"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment