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 9133655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:31:31+00:00 2026-06-17T08:31:31+00:00

I am looking for an algorithm that can map a number to a unique

  • 0

I am looking for an algorithm that can map a number to a unique permutation of a sequence. I have found out about Lehmer codes and the factorial number system thanks to a similar question, Fast permutation -> number -> permutation mapping algorithms, but that question doesn’t deal with the case where there are duplicate elements in the sequence.

For example, take the sequence ‘AAABBC’. There are 6! = 720 ways that could be arranged, but I believe there are only 6! / (3! * 2! * 1!) = 60 unique permutation of this sequence. How can I map a number to a permutation in these cases?

Edit: changed the term ‘set’ to ‘sequence’.

  • 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-06-17T08:31:32+00:00Added an answer on June 17, 2026 at 8:31 am

    From Permutation to Number:

    Let K be the number of character classes (example: AAABBC has three character classes)

    Let N[K] be the number of elements in each character class. (example: for AAABBC, we have N[K]=[3,2,1], and let N= sum(N[K])

    Every legal permutation of the sequence then uniquely corresponds to a path in an incomplete K-way tree.

    The unique number of the permutation then corresponds to the index of the tree-node in a post-order traversal of the K-ary tree terminal nodes.

    Luckily, we don’t actually have to perform the tree traversal — we just need to know how many terminal nodes in the tree are lexicographically less than our node. This is very easy to compute, as at any node in the tree, the number terminal nodes below the current node is equal to the number of permutations using the unused elements in the sequence, which has a closed form solution that is a simple multiplication of factorials.

    So given our 6 original letters, and the first element of our permutation is a ‘B’, we determine that there will be 5!/3!1!1! = 20 elements that started with ‘A’, so our permutation number has to be greater than 20. Had our first letter been a ‘C’, we could have calculated it as 5!/2!2!1! (not A) + 5!/3!1!1! (not B) = 30+ 20, or alternatively as
    60 (total) – 5!/3!2!0! (C) = 50

    Using this, we can take a permutation (e.g. ‘BAABCA’) and perform the following computations:
    Permuation #= (5!/2!2!1!) (‘B’) + 0(‘A’) + 0(‘A’)+ 3!/1!1!1! (‘B’) + 2!/1!

    = 30 + 3 +2 = 35

    Checking that this works: CBBAAA corresponds to

    (5!/2!2!1! (not A) + 5!/3!1!1! (not B)) ‘C’+ 4!/2!2!0! (not A) ‘B’ + 3!/2!1!0! (not A) ‘B’ = (30 + 20) +6 + 3 = 59

    Likewise, AAABBC =
    0 (‘A’) + 0 ‘A’ + ‘0’ A’ + 0 ‘B’ + 0 ‘B’ + 0 ‘C = 0

    Sample implementation:

    import math
    import copy
    from operator import mul
    
    def computePermutationNumber(inPerm, inCharClasses):
        permutation=copy.copy(inPerm)
        charClasses=copy.copy(inCharClasses)
    
        n=len(permutation)
        permNumber=0
        for i,x in enumerate(permutation):
            for j in xrange(x):
                if( charClasses[j]>0):
                    charClasses[j]-=1
                    permNumber+=multiFactorial(n-i-1, charClasses)
                    charClasses[j]+=1
            if charClasses[x]>0:
                charClasses[x]-=1
        return permNumber
    
    def multiFactorial(n, charClasses):
        val= math.factorial(n)/ reduce(mul, (map(lambda x: math.factorial(x), charClasses)))
        return val
    

    From Number to Permutation:
    This process can be done in reverse, though I’m not sure how efficiently:
    Given a permutation number, and the alphabet that it was generated from, recursively subtract the largest number of nodes less than or equal to the remaining permutation number.

    E.g. Given a permutation number of 59, we first can subtract 30 + 20 = 50 (‘C’) leaving 9. Then we can subtract ‘B’ (6) and a second ‘B'(3), re-generating our original permutation.

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

Sidebar

Related Questions

I am looking at an algorithm that can map between characters with diacritics (
I'm looking for a good algorithm that can give me the unique edges from
I'm looking for an algorithm that can check if nested regex repeats are reducible.
I'm looking for an algorithm that given two permutations of a sequence (e.g. [2,
I am looking for a sound algorithm that would randomly place a given number
I have a relatively simple algorithm that walks an std::vector looking for two neighbouring
I am looking for an algorithm (php would be most ideal) that can, given
I was looking for an effective algorithm that can give me an accurate idea
I'm looking for an easy to use library/algorithm that can sync two folders locally
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily

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.