Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 606049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:12:45+00:00 2026-05-13T17:12:45+00:00

How can I generate the shortest sequence with contains all possible permutations? Example: For

  • 0

How can I generate the shortest sequence with contains all possible permutations?

Example:
For length 2 the answer is 121, because this list contains 12 and 21, which are all possible permutations.

For length 3 the answer is 123121321, because this list contains all possible permutations:
123, 231, 312, 121 (invalid), 213, 132, 321.

Each number (within a given permutation) may only occur once.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:12:46+00:00Added an answer on May 13, 2026 at 5:12 pm

    This greedy algorithm produces fairly short minimal sequences.

    UPDATE: Note that for n ≥ 6, this algorithm does not produce the shortest possible string!

    • Make a collection of all permutations.
    • Remove the first permutation from the collection.
    • Let a = the first permutation.
    • Find the sequence in the collection that has the greatest overlap with the end of a. If there is a tie, choose the sequence is first in lexicographic order. Remove the chosen sequence from the collection and add the non-overlapping part to the end of a. Repeat this step until the collection is empty.

    The curious tie-breaking step is necessary for correctness; breaking the tie at random instead seems to result in longer strings.

    I verified (by writing a much longer, slower program) that the answer this algorithm gives for length 4, 123412314231243121342132413214321, is indeed the shortest answer. However, for length 6 it produces an answer of length 873, which is longer than the shortest known solution.

    The algorithm is O(n!2).

    An implementation in Python:

    import itertools
    
    def costToAdd(a, b):
        for i in range(1, len(b)):
            if a.endswith(b[:-i]):
                return i
        return len(b)
    
    def stringContainingAllPermutationsOf(s):
        perms = set(''.join(tpl) for tpl in itertools.permutations(s))
        perms.remove(s)
        a = s
        while perms:
            cost, next = min((costToAdd(a, x), x) for x in perms)
            perms.remove(next)
            a += next[-cost:]
        return a
    

    The length of the strings generated by this function are 1, 3, 9, 33, 153, 873, 5913, … which appears to be this integer sequence.

    I have a hunch you can do better than O(n!2).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can generate all permutations from a list, using glob or Algorithm::Permute
By using the android package manager I can generate a list of all installed
ASP.NET MVC can generate HTML elements using HTML Helpers, for example @Html.ActionLink() , @Html.BeginForm()
I can generate a few lines of code that will do this but I'm
In C# I can generate random number from a string like this: public string
The code here can generate numbers like this [1 -2 3 -4 5 -6
Users of my website can generate a custom form. All the fields are saved
With gcc and gfortran I can generate a list of preprossesor macros defined by
we know that we can generate EDMX model from sql server because EF support
I found you can generate this in SOAP in php: <foo bar=blah>12345</foo> With this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.