Js used to prove safety brush offer (string arrangement)

Title Description

Description Title
input a character string to print all the characters in the string are arranged in lexicographic order. For example input string abc, abc print all strings of characters a, b, c can be arranged out, acb, bac, bca, cab and cba.
Input Description:
enter a string of not more than 9 (possibly repeated characters), characters include only lowercase letters.

Thinking

Based on the idea of ​​backtracking
7578108_1499250116235_8F032F665EBB2978C26C4051D5B89E90

Cattle off network link

js code

function Permutation(str)
{
    // write code here
    if (str === '') return []
    let arr = str.split('')
    let res = []
    Help(arr, 0)
    return res.sort()
    
    function Help(arr, i) {
        if (i === arr.length-1) {
            if (!res.includes(arr.join(''))) {
                res.push(arr.join(''))
                return
            }
            
        }
        for(let j = i; j < arr.length; j++) {
            swap(arr, i, j)
            Help(arr, i+1)
            swap(arr, i, j)
        }
    }
    
    function swap (arr, i, j) {
        let temp = arr[i]
        arr[i] = arr[j]
        arr[j] = temp
    }
}

Guess you like

Origin www.cnblogs.com/dpnlp/p/yongjs-shua-jian-zhioffer-zi-fu-chuan-de-pai-lie.html