Skip to content

Instantly share code, notes, and snippets.

@harunorimurata
Last active April 18, 2023 03:36
Show Gist options
  • Save harunorimurata/205b2f83278b2d77f308d500c387b2a2 to your computer and use it in GitHub Desktop.
Save harunorimurata/205b2f83278b2d77f308d500c387b2a2 to your computer and use it in GitHub Desktop.
func Repeat(s string, n uint) string {
switch n {
case 0:
return ""
case 1:
return s
}
max := len(s) * int(n)
var b strings.Builder
b.Grow(max)
b.WriteString(s)
for ; 2 <= n; n = n >> 1 {
b.WriteString(b.String())
}
b.WriteString(b.String()[:max-b.Len()])
return b.String()
}
const repeat = (s, n) => {
if (n === 0) return '';
else if (n === 1) return s;
const max = s.length * n;
for (; 2 <= n; n = n >> 1) { s += s; }
return s + s.substring(0, max - s.length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment