Skip to content

Instantly share code, notes, and snippets.

@robherley
Created March 16, 2018 18:29
Show Gist options
  • Save robherley/947267075ff602ede7e91b7eb744b819 to your computer and use it in GitHub Desktop.
Save robherley/947267075ff602ede7e91b7eb744b819 to your computer and use it in GitHub Desktop.
const MOD = 1000000007;
const samSubstr = balls =>
[...balls].reduceRight(
(prev, digit, i) => {
return [
(prev[0] * 10 + 1) % MOD,
(prev[1] + ~~digit * prev[0] * (i + 1)) % MOD
];
},
[1, 0]
)[1];
console.log(samSubstr('16')); // 23
console.log(samSubstr('23')); // 164
console.log(samSubstr('1337')); // 1904
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment