Skip to main content
5 of 5
Added an alternative
Kamil Drakari
  • 4.4k
  • 11
  • 21

Japt v2.0a0, 21 bytes

qW ÔË+WpE>VÃw
qWpUʧV

Try it

Input as string, n, substring

v2.0a0 is necessary because v1.4.6 errors if you try to use p this way.

Explanation:

qW ÔË+WpE>VÃw
qW            # Split <string> where <substring> appears
   Ô          # Reverse the array
    Ë      Ã  # For each item in the array:
     +        #  Append:
      Wp      #   <substring> repeated a number of times equal to:
        E>V   #    The current index is greater than <n>
              #    (Boolean gets converted to 1 or 0)
            w # Reverse the new array
              # Store as U

qWpUʧV
q             # Turn U into a string by inserting this between each item:
 Wp           #  <substring> repeated a number of times equal to:
   UÊ         #   Length of U (i.e. 1 + number of times <substring> appeared)
     §V       #   Is less than or equal to <n>
              # Output that string

I've tried an alternate using ð but the best I got was 25 bytes.

Kamil Drakari
  • 4.4k
  • 11
  • 21