Rakuten Interview Question

Basic string repetition question and it's optimisation.