Created
June 7, 2018 14:22
-
-
Save ycmjason/e3af4f23cd7a670f6f3b1178688f9e67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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