Skip to content

Instantly share code, notes, and snippets.

@or9
Created January 5, 2021 22:13
Show Gist options
  • Save or9/acfd3e02868264ede44b1623c79c3315 to your computer and use it in GitHub Desktop.
Save or9/acfd3e02868264ede44b1623c79c3315 to your computer and use it in GitHub Desktop.
Hackerrank stuff
function repeatedString(s, n) {
if (n < 1) return 0;
let initialMatches = s.match(/a/gm);
// Hackerrank is not down with nullish coalesce or even optional chaining AT ALL
// Otherwise, you could simply say if (!initialMatches?.length)
if (!initialMatches || !initialMatches.length) return 0;
if (n < initialMatches.length) {
return n;
}
let aCount = Math.floor(n / s.length) * initialMatches.length;
let remainder = n % s.length;
if (remainder) {
// No optional chaining and definitely no nullish coalesce on Hackerrank
// was throwing on a runtime error trying to read length of undefined. Stupid. Since "Test against custom input" does NOTHING, and we're not allowed to see input/output, just run it in the terminal against your own test cases.
// aCount += s
// .slice(0, remainder)
// .match(/a/gm)?
// .length
// ?? 0;
const remainingAs = s.slice(0, remainder).match(/a/gm) || [];
const r = remainingAs.length;
aCount += r;
}
return aCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment