The input is a word of lowercase letters not separated by whitespace. A newline at the end is optional.

The same word must be output in a modified version: For each character, double it the second time it appears in the original word, triple it the third time etc.

Example input:


Example output:


Standard I/O rules apply. The shortest code in bytes wins.

Tests provided by @Neil :

tutu -> tuttuu
queue -> queuuee
bookkeeper -> boookkkeeepeeer
repetitive -> repeetittiiveee
uncopyrightables -> uncopyrightables
abracadabra -> abraacaaadaaaabbrraaaaa
mississippi -> misssiisssssssiiipppiiii

42 Answers 42


Python, 66 62 bytes

Try it here


def s(w,o=[],n=""):
 for x in w:o+=x;n+=x*o.count(x)
 return n


def stretch(word):
    occurrences = []
    newWord = ""
    for w in word:
        newWord += w * occurrences.count(w)
    return newWord

Vyxal sr, 4 bytes


Try it Online!


Knight, 77 bytes

;=x=yP;Wx;E++"=x"A Ax" 0"=xSxF1""Wy;E++++"O+*A Ay=x"=aA Ay"+1x"a"'\'"=ySyF1""

Try it online!


; = x (= y PROMPT)
    ; EVAL (+ (+ "=x" (ASCII (ASCII x))) " 0")
    : = x (SUBSTITUTE x 0 1 "")
    ; EVAL (+ (+ (+ (+ "O+*A Ay=x" (= a (ASCII (ASCII y)))) "+1x") a) "'\'")
    : = y (SUBSTITUTE y 0 1 "")

BQN, 7 6 bytes


Anonymous tacit function. Try it at BQN online!


     ⊒   For each character, how many times has it appeared previously?
   1+    Add 1 to each
 /˜      Replicate that many times the elements of
⊢        The original input

Ruby, 50 48 bytes

Anonymous function.


Go, 142 bytes

No awards, but a neat exercise.

func f(s string){m:=map[rune]int{}
for _,c:=range s{if _,ok:=m[c];!ok{m[c]=1}

You can save 1 byte by changing for _,c to for i,c and changing string(c) to s[i:i+1], but then it works only with ASCII.


Factor, 120 bytes

[ dup dup length iota [ head* ] with map reverse zip [ first2 over [ = ] curry count swap <array> >string ] map concat ]

Ungolfed, explained:

! "bonobo" -> { "b" "bo" "bon" "bono" "bonob" "bonobo" }
: ascend-string ( string -- string ascending-seq )
    dup dup length iota [ head* ] with map reverse ;

: stretch-word ( string -- stretched )
    ascend-string zip        ! zip string with ascending version of itself
        first2 over          ! get each string index and its ascended version 
        [ = ] curry count    ! count how many times this letter has appeared
        swap <array> >string ! >string
    ] map concat ;           ! "" join

Translation of: @KennyLau's Pyth answer.


Swift, 134 bytes

var c = NSCountedSet(),n = ""
i.characters.forEach({var s = String($0);c.addObject(s);for _ in 0..<c.countForObject(s){n.append($0)}})

Assumes Foundation has been implicitly imported. i is the input of type String, n is the output of type String.

I'm falling back on Foundation classes here as NSCountedSet gives the the character count lookup behavior we want. The problem with mixing Swift and Objective-C types is that Swift structs do not conform to NSObjectProtocol. We see this issue when trying to store a Swift Character (String.CharacterView) in an NSCountedSet, which is expecting objects of type AnyObject (or NSObject). I lose around 17 bytes having to create a String from each Character in the input string.

I also lose some bytes having to create a for loop to append the new characters based on their count in the set. I can't use forEach here since the return type of countForObject is an Int, not a collection type. There is room for improvement here.

Swift String types are immutable, so I can't explicitly mutate the input. There may be room for improvement by using an NSMutableString, but the characters lost by explicitly declaring the input of that type and calling the appendString method may add more characters to the program.


Java 10, 220 94 bytes

s->{var m=new int[123];for(var c:s.toCharArray())for(int i=++m[c];i-->0;System.out.print(c));}

Try it online.


s->{                   // Method with String parameter and no return-type
  var m=new int[123];  //  Integer-array containing 123 zeros
  for(var c:s.toCharArray())
                       //  Loop over the characters of the input-String
    for(int i=++m[c];  //   Increase the count of the current character in the array first,
                       //   and set `i` to this count
        i-->0;         //   Inner loop that many times
                       //    And print that many times the current character `c`

PHP, 46 bytes


Run as pipe with -nR or try it online.

These 42 bytes print the same, but the loop is infinite (which results in a timeout error):


printf solution, 47 bytes:


Side note: PHP used to yield notices for undefined constants and cast them to string for decades. PHP 7.2 yields warnings with a hint that, in future versions, they will throw fatal errors.

End of an era. :-/ I wonder if they will do the same to undefined variables.


Kotlin, 60 bytes


Try it online!

  • \$\begingroup\$ This isn't quite valid; you aren't supposed to double each time the letter reappears, but just increase the number of repetitions by 1. So aaa should become aaaaaa ([a] [aa] [aaa]), whereas yours outputs aaaaaaa ([a] [aa] [aaaa]). \$\endgroup\$
    – hyper-neutrino
    Commented Nov 20, 2021 at 21:20

brev, 46 bytes

(with(ct*'a 0)(as-list flatten(over(it x x))))

If output like ("m" "i" "s" "ss" "ii" "sss" "ssss" "iii" "p" "pp" "iiii") were legal you could cut out the as list flatten for seventeen bytes.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.