Logic topic, given 26! The letters are arranged (regularly), and the results of the arrangement are obtained for any number.

if there is an abc, there are 3! The order of
is abc acb bac bca cab cba
to any number, such as Numeric 4, output bca.

now there are 26 English letters, a total of 26! Given any number N, how to find the alphabetic column?

listen to others say that it can be solved by recursion, but I really can"t think of any solution

Apr.11,2021

// count 
var counts = Object.create(null)
function getCount(len){
    if(!counts[len]){
        var count = 1;
        while(len > 1){
            count *= len
            len--
        }
        counts[len] = count
    }
    return counts[len]
}

var getStr = function(arr){
    //if(!Array.isArray(arr))return;
    if(Object.prototype.toString.call(arr) !== "[object Array]")return;
    
    var len = arr.length
    
    return function(n){
        if(n < 1){
            alert('')
            return false
        }
        if(n > getCount(len = arr.length)){
            alert('')
            return false
        }
        n -= 1;//0
        
        var index = 0,
            newArr = arr.slice(),
            resulte = [],
            count = 0
        
        //
        while(len > 1){
            //
            count = getCount(len)
            //n
            //5120110(0)
            // 110*5/120 | 0 = 44e
            index = n*len/count | 0
            //
            resulte.push(newArr.splice(index,1))
            //4 4*120/5 
            // 
            // n = 110 - 4*120/5 = 14
            n -= index*count/len
            len--
        }
        //
        len == 1 && resulte.push(newArr[0])
        
        console.log(resulte.join(''))
    }
}(['a','b','c','d','e'])

getStr(1); // 'abcde'
getStr(4); // 'abdec'
getStr(120); // 'edcba'
getStr(119); // 'edcab'

Recursion can be solved. To put it this way, the idea of recursion is to turn a big problem into a small problem, so how to divide this problem? Take abcd as an example.

because the ranking results have a total of 4! Species, and because it is in order, then:

  • there are four possibilities for the first place, and the remaining three have a total of 3! Species
  • there are three possibilities for the second place, and the remaining two have a total of 2! Species
  • there are two possibilities for the third place, and the remaining one has a total of 1! Species

so, suppose N = 9, then according to the above process, if the first place is a, then the rest of the bits have a total of 3! It's possible that because 3! < 9, then the first place must not be a, so we have to increment one bit, change it to b, plus the combination number of the previous a, 3! + 3! > 9, so no matter what the final arrangement is, the first place in this arrangement must be b, and then the size of the problem is reduced, which is equivalent to that in the three letters of acd, the result of the arrangement of N = 3 until you find the letters of all bits (that is, when N = 0).

the conversion process goes something like this (* for unknown letters):

  • s = xxxx, N = 9
  • s = bxxx, N = 3
  • s = bcxx, N = 1
  • s = bcad, N = 0

if there is a mistake, please correct it, and the god will spray it gently


interesting, when I look at the topic, all I can think of is to first calculate all the permutations into an array, and then any number N is indexed directly from this array to be the result.
my current idea is that if the law is fixed, that is, abc acb bac bca cab cba , split the 26 letters into an array, and each letter should have an algorithm under the corresponding number to get the displacement target, but the specific algorithm still has to take time to think about it and realize the pursuit of

. < hr >

hfhan has given an implementation algorithm, which is excellent


https://blog.csdn.net/u010271.
the result in this example is the last array of results. If there is a problem with the order, call the sort () method directly, which is an alphabetical array.
then index accesses the result array is the desired result.

Menu