emanresu A posted a challenge to reconstruct an ordered list of integers from its unordered set of unordered prefixes. Many users (beginning with xnor's answer) noticed that summing each unordered prefix led to a very short solution. Well, let's cruelly take that possibility away by using data that isn't integers!

A few answers from the other challenge work for this version as well; that's no problem. I'm just hoping to stimulate other answers in other languages.

(The text below is a slightly modified but mostly copied version of emanresu A's original text.)

A list of strings \$[a_1,a_2,a_3, \ldots, a_n]\$ can be uniquely represented as an unordered list of its unordered prefixes \$ [[a_1],[a_1, a_2], [a_1,a_2,a_3], \ldots, [a_1,a_2,a_3, \ldots, a_n]] \$. Both the set of prefixes and the set of elements of each individual prefix can be in any order - for example,


could be represented by any of

[["lorem"],["lorem","ipsum"],["lorem","ipsum","dolor"]] or
[["dolor","ipsum","lorem"],["ipsum","lorem"],["lorem"]] or

and these are all equivalent up to order and all represent ["lorem","ipsum","dolor"].

Your challenge is, given an unordered list of the unordered prefixes of a (non-empty) list of strings, to return the ordered list represented by them. The list may contain repeated items. You may stipulate that the input includes the empty prefix if desired. This is , so shortest code in each language wins!


Note that the order of the output does matter: for the third test case, for example, you must output ["consectetuer","sit","elit","adipiscing"], not ["adipiscing","sit","elit","consectetuer"].

-> ["lorem","ipsum","dolor"]

-> ["lorem","ipsum","dolor"]

-> ["consectetuer","sit","elit","adipiscing"]

-> ["lorem","ipsum","consectetuer","dolor","ipsum"]

-> ["ipsum","justo","nunc","in","molestie"]

-> ["sit","elit","sit","elit"]

-> ["non","lectus","phasellus","pede","ipsum","sit","nunc"]

-> ["pellentesque","semper","vestibulum","ullamcorper"]
  • \$\begingroup\$ May i assume each string contains only letters? \$\endgroup\$
    – tsh
    Mar 4 at 5:52
  • \$\begingroup\$ @tsh I'm fine with that. \$\endgroup\$ Mar 4 at 6:54
  • \$\begingroup\$ Does this answer your question? Reconstruct a list from its prefixes \$\endgroup\$
    – noodle man
    Mar 4 at 12:18

15 Answers 15


Python 2, 73 68 bytes

lambda L:reduce(lambda v,l:map(l.remove,v)and v+l,sorted(L,key=len))

Try it online!

An anonymous function that accepts a List of lists and returns a list as result.

Copying my answer from the original question.

  • -5 bytes thank to @Mukundan314

How does it work?

With the input sorted by length (so [["ipsum","lorem"],["dolor","lorem","ipsum"],["lorem"]] becomes [["lorem"],["ipsum","lorem"],["dolor","lorem","ipsum"]]), for each sublist, remove the first occurrence of elements already visited from l and mark the remaining element as visited.

It works for duplicates.

  • 1
    \$\begingroup\$ -5 bytes by doing v+l instead of v+[l[0]] \$\endgroup\$ Mar 4 at 13:51
  • \$\begingroup\$ @Mukundan314 freaking awesome! \$\endgroup\$
    – enzo
    Mar 4 at 15:22
  • \$\begingroup\$ 67 with *0+v+l \$\endgroup\$
    – no comment
    Mar 7 at 16:04

Haskell, 57 bytes

import Data.List

Try it online!

Hey, I knew my slightly different approach would come in handy! Only change is needing to sort by length instead of sum, 'cause uh... well you know.


Nekomata, 8 bytes


Attempt This Online!

The input includes the empty prefix.

↕           Take a permutation of the input (non-deterministically)
 ᵉt         Take its tail (removing the first item)
   i        and its initial prefix (removing the last item)
    ᶻ{      Zip with:
      ∕         Set minus
       z        Check that the result is a singleton

Nekomata, 9 bytes


Attempt This Online!

The input does not include the empty prefix.

Using @xnor's trick in emanresu A's challenge.

ᵐ∑          Take the sum of each item of the input
            Strings are automatically converted to code points and padded with zeros
  o         Sort
   ç        Prepend zero
    ∆       Take the differences
     ᵐᶠZ    Remove zeros
        H   Convert the code points to characters
  • 1
    \$\begingroup\$ Ha, your 9-byte found a way to bust out of Greg's guard rails :) \$\endgroup\$
    – Jonah
    Mar 4 at 18:43

Jcram, 18 bytes


JavaScript (Node.js), 100 bytes


Try it online! Uses a similar method to movatica's python answer.

  • \$\begingroup\$ Well done! Golfing with JCram is probably not so easy :P. \$\endgroup\$ Apr 18 at 15:27

JavaScript (ES6), 77 bytes


Try it online!



Try it online!


Jelly, 7 bytes


A monadic Link that accepts the prefixes as a list of lists of lists of characters (including an empty prefix) and yields a list of lists of characters.

Try it online! Or see the test-suite.


LÞœ^ƝṪ€ - Link: Prefixes
 Þ      - sort by:
L       -   length
    Ɲ   - for neighbouring pairs:
  œ^    -   multiset symmetric difference
     Ṫ€ - tail of each
  • 2
    \$\begingroup\$ You're allowed to take an empty list in the list of prefixes, so you can remove the Ż for 7 \$\endgroup\$
    – emanresu A
    Mar 4 at 1:37
  • \$\begingroup\$ Thanks @emanresuA. \$\endgroup\$ Mar 4 at 16:51

J, 25 bytes


Attempt This Online! Setup borrowed from Jonah's answer.

Sort by length, join adjacent prefixes, take the element that appears an odd number of times.

J, 27 bytes


Attempt This Online!

Sort each prefix, sort by length. For each pair adjacent prefixes: Take the first different value from the longer prefix.

  • \$\begingroup\$ Very nice insight \$\endgroup\$
    – Jonah
    Mar 5 at 14:30

05AB1E, 13 bytes


Try it online or verify all test cases.


¯              # Push an empty list; result-list starts with []
 I             # Push the input-list of lists
  é            # Sort it by length (shortest to longest)
   v           # Pop and loop over each inner list `y`:
    D          #  Duplicate the current result-list
     y         #  Push the current loop-list `y`
      s        #  Swap so the duplicated result-list is at the top
       õ.;     #  Replace every first occurrence in `y` with an empty string
          õK   #  Then remove those empty strings
            «  #  And merge the remaining single item to the result-list
               # (after the loop, the result-list is output implicitly)

Perl 5, 83 77 bytes


Try it online!


Python, 76 75 74 bytes

-2 bytes thanx to Mukundan314

lambda p,l=[]:[[*map(q.remove,l),l:=q+l][-1][0]for q in sorted(p,key=len)]

Attempt This Online!

  • \$\begingroup\$ 72 bytes: lambda p,l=[]:[[q,*map(q.remove,l),l:=l+q][0]for q in sorted(p,key=len)]; I don't remember if this is an acceptable output format though; fixing it would requires 3 bytes tying it with the current 75 bytes \$\endgroup\$ Mar 4 at 7:36
  • 1
    \$\begingroup\$ 74 bytes: lambda p,l=[]:[[*map(q.remove,l),l:=q+l][-1][0]for q in sorted(p,key=len)]; this one doesn't have any caveats \$\endgroup\$ Mar 4 at 7:40

J, 40 bytes


Attempt This Online!

This approach is more or less a port of Arnauld's recursive approach.

J, 45 bytes


Try it online!

Kinda long, but possibly interesting approach to dealing with repeats:

  • (a:-.~&,<\/.~)&.> A pre-processing step that goes through each set and replaces repeated items by actually repeating them. This allows our main code to assume uniqueness.
    • Eg, conceptually, if we have a list like [a, b, a], it becomes [a, b, [a, a]]. We use nested J boxes for this.
  • The remaining steps may now assume no repeats.
  • a:,]/:#&> Sort the sets by their length, and add an empty set to start
  • 2...-.~&>/\ Do consecutive set differences to recover the original list
  • ~.&>@ Uniquify each to undo the effect of the first pre-processing step
  • ,@ Flatten the result

Python, 69 bytes

lambda p:[s for s,in sorted(p,key=len)if[r.remove(s)for r in p if r]]

Attempt This Online!

Python, 73 bytes

lambda p:p and(m:=min(p,key=len))+f([q for q in p if q!=m!=q.remove(*m)])

Attempt This Online!


Charcoal, 23 bytes


Try it online! Link is to verbose version of code. Accepts but does not require the empty prefix. Explanation:


While there is a prefix with one more member than the number of terms collected so far, ...


... find any terms that appear more times in that prefix than in the terms collected so far and push one to the (predefined empty) list of terms so far.


Output the final list.

Differences between this solution and the numeric solution:

  • Slightly different way of retrieving the next prefix, since I need to refer to it twice
  • Different method of finding the next term, of course
  • Resulting list is of strings so only takes 1 byte to output

Python, 101 bytes

lambda l,p=0:[PyObj_FromPtr(-p+(p:=x))for x in sorted(sum(map(id,b))for b in l)]
from _ctypes import*

Attempt This Online!

-6 bytes thanks to Mukundan314.

Direct adaptation of xnor's answer for integers, converting the strings to and from integers using id and its inverse _ctypes.PyObj_FromPtr.

  • \$\begingroup\$ -6 bytes by replacing id(a)for a in b with map(id,b) \$\endgroup\$ Mar 4 at 14:01

APL+WIN, 58 bytes

Prompts for a nested vector of nested vectors of strings.


Try it online! Thanks to Dyalog Classic


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.